tros804 Posted March 7, 2019 Share Posted March 7, 2019 Hello I'm currently building a GUI for a user that'll run a command against our file share server (once the permissions are set) and display who has a lock on certain files. The command part is running just fine, however, I'm having a hard time getting only parts of the data to appear in the GUI. What I'd like to see is when the GUI is launched, it display only the opened PDF file name (not the entire N:\ folder) as well as the user that has a lock on it. I've got it as far as displaying only the filename (without all of the extra clutter in front) but can't seem to get the username to appear (which so happens to be on the very next line). Ideally, I'd like for the username to appear to the right of the filename as the end result once I get this part working would be to add a button to allow the user to kick users out if unable to get a hold of them (which is the reason for the list box so said user can select a certain file). Below is the code I currently have and the example text file I'm working with. Any help is appreciated. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiListBox.au3> #include <array.au3> #include <File.au3> $drive = EnvGet("systemdrive") FileQuery() Func FileQuery() ; Create a GUI with various controls. $hGUI = GUICreate("Who Has A Calendar Open?", 400, 300, 350, 350) $g_hListBox = _GUICtrlListBox_Create($hGUI, "", 2, 2, 396, 200) $idQuery = GUICtrlCreateButton("Refresh Query", 85, 225, 85, 25) $idExit = GUICtrlCreateButton("Exit", 225, 225, 85, 25) $command = Run (@ComSpec & ' /c psfile.exe \\servername "N:\share\Updated Monthly Schedules" > %systemdrive%\temp\temp.txt', "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD) ProcessWaitClose($command) $display = StdoutRead($command) ; Find .pdf in output $file = $drive & "\temp\temp.txt" For $i = 1 to _FileCountLines($file) $line = FileReadLine($file, $i) If StringInStr($line, ".pdf") Then $after = StringSplit($line, "\")[6] $user = StringInStr($line, "User") _GUICtrlListBox_AddString($g_hListBox, $after) ;Create List box with data EndIf Next ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE FileDelete($file) ExitLoop Case $idExit FileDelete($file) Exit EndSwitch WEnd ; Delete the previous GUIs, all controls, and temp file. GUIDelete($hGUI) EndFunc Files opened remotely on servername matching N:\share\Updated Monthly Schedules: [10429393] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read [10429406] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read [10429409] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10429413] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10430671] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019\wal-mart supercenter 16.pdf User: STEPHENG Locks: 0 Access: Read [10568007] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017 User: ERICKT Locks: 0 Access: Read [10568013] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017 User: ERICKT Locks: 0 Access: Read Link to comment Share on other sites More sharing options...
mikell Posted March 7, 2019 Share Posted March 7, 2019 (edited) The following assumes that there can be several pdf/user couples present in the .txt file expandcollapse popup#Include <Array.au3> $txt = "Files opened remotely on servername matching N:\share\Updated Monthly Schedules:" & @crlf & _ @crlf & _ "[10429393] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019" & @crlf & _ " User: STEPHENG" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10429406] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019" & @crlf & _ " User: STEPHENG" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10429409] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019" & @crlf & _ " User: STEPHENG" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10429413] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019" & @crlf & _ " User: STEPHENG" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10430671] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019\wal-mart supercenter 16.pdf" & @crlf & _ " User: STEPHENG" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10568007] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017" & @crlf & _ " User: ERICKT" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10430671] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019\wal-mart supercenter 17.pdf" & @crlf & _ " User: ERICKT" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read " & @crlf & _ "[10568013] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017" & @crlf & _ " User: ERICKT" & @crlf & _ " Locks: 0" & @crlf & _ " Access: Read" ; Msgbox(0,"", $txt) Local $s, $a = StringRegExp($txt, '([^\\]+pdf)[^:]+:\h*(\N+)', 3) ;_ArrayDisplay($a) For $i = 0 to UBound($a)-2 step 2 $s &= $a[$i] & " -- " & $a[$i+1] & @crlf Next Msgbox(0,"", $s) Edited March 7, 2019 by mikell FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 7, 2019 Share Posted March 7, 2019 (edited) Hi @tros804, and welcome to the AutoIt forums You can use something like this: expandcollapse popup#include <FileConstants.au3> #include <StringConstants.au3> Global $strFileContent, _ ; Content of the File to read $arrPDFFiles, _ ; Array in which will be stored (if present) all the lines with .pdf and User in those $arrResult, _ ; Result of the SRE $strFileName = "", _ ; File Name (locked by some user) $strUsername = "" ; Username who locks the PDF file ; Reading the content of the file $strFileContent = FileRead(@ScriptDir & "\SampleText.txt") ; Obtaining the list of PDF files $arrPDFFiles = StringRegExp($strFileContent, '\[\d*\].*\.pdf\s*User:\s*\w*', $STR_REGEXPARRAYGLOBALMATCH) ; If there are some (PDFs) If IsArray($arrPDFFiles) Then For $i = 0 To UBound($arrPDFFiles) - 1 Step 1 ; Obtaining the full filename and user who is locking it $arrResult = StringRegExp($arrPDFFiles[$i], '\[\d*\](.*\.pdf)\s*User:\s*(\w*)', $STR_REGEXPARRAYMATCH) ; Obtaining only the name of the PDF $strFileName = _PathSplitEx($arrResult[0]) ; Obtain the user who is locking the PDF file $strUsername = $arrResult[1] ; Printing the result ConsoleWrite($i + 1 & ": '" & $strFileName & "' locked by '" & $strUsername & "'" & @CRLF) Next EndIf Func _PathSplitEx($strPath) Local $strDrive, _ $strDir, _ $strFileName, _ $strExtension If IsArray(_PathSplit($arrResult[0], $strDrive, $strDir, $strFileName, $strExtension)) Then Return $strFileName & $strExtension EndFunc Sample file content: Spoiler [10429393] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read ? [10429406] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read [10429409] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10429413] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10430671] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019\wal-mart supercenter 16.pdf User: STEPHENG Locks: 0 Access: Read ? [10568007] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017\somepdf.pdf User: ERICKT Locks: 0 Access: Read [10568013] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017 User: ERICKT Locks: 0 Access: Read Comments are there for a reason, so, just look at those Edited March 7, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Nine Posted March 7, 2019 Share Posted March 7, 2019 By using @mikell solution, here what it should look like putting all together : $command = Run (@ComSpec & ' /c psfile.exe \\servername "N:\share\Updated Monthly Schedules"', "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD) ProcessWaitClose($command) $display = StdoutRead($command) $array = StringRegExp($display, '([^\\]+pdf)[^:]+:\h*(\N+)', 3) For $i = 0 to UBound($array)-2 step 2 _GUICtrlListBox_AddString($g_hListBox, $array[$i] & " -- " & $array[$i+1]) ;Create List box with data Next Untested, but should be close enough... “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
tros804 Posted March 7, 2019 Author Share Posted March 7, 2019 Thank you all for your input. I hit a huge brick wall with this one. @FrancescoDiMuro Your solution is exactly what I was trying to accomplish. Thanks for the input! And as an added bonus, your solution eliminates the command getting exported to a temporary text file which was another brick wall I hit. My first time posting to the forum (after being a lurker for many years) and I'm quite pleased with the prompt assistance I received! You guys rock! Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted March 7, 2019 Share Posted March 7, 2019 @tros804 Happy to have helped, and happy to let you have a warm welcome Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
tros804 Posted December 9, 2019 Author Share Posted December 9, 2019 Ok folks, I apologize for bringing up an old topic but the user I built this for has been having a slight issue lately... While the original code still works, we're finding that files open in ALL CAPS aren't appearing. See screenshot below of running the psfile command in a standard command prompt. While I don't understand why some show in all caps, the fact is that the code that I was assisted with earlier this year doesn't appear to take into account the cap size. I'm not sure how to have it search for .pdf or .PDF in the same command. Thoughts? Below is the current code I'm working with... expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** #AutoIt3Wrapper_Icon=test_.ico #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiListBox.au3> #include <array.au3> #include <File.au3> #include <StringConstants.au3> #include <AutoItConstants.au3> $useraccount = EnvGet("userprofile"); Sets the root of the User Profile If Not FileExists($useraccount & "\psfile.exe") Then ; If psfile.exe does not exist in C:\Users\%username% then copy it from S Drive. Otherwise, move to registry query. FileCopy("\\server\share\everyone\UPDATES\PSfile\psfile.exe", $useraccount) RegQuery() Else RegQuery() EndIf Func RegQuery() If Not RegRead("HKCU\Software\Sysinternals\PsFile", "EulaAccepted") Then ; If EulaAccepted registry key does not exist, create it. Otherwise, move to the GUI. RegWrite("HKCU\Software\Sysinternals\PsFile", "EulaAccepted", "REG_DWORD", "1") FileQuery() Else FileQuery() EndIf EndFunc Func FileQuery() ; Create a GUI with various controls. $hGUI = GUICreate("Who Has A Calendar Open?", 400, 300, 350, 350) $g_hListBox = _GUICtrlListBox_Create($hGUI, "", 2, 2, 396, 200) $idQuery = GUICtrlCreateButton("Refresh Query", 40, 225, 85, 25) $idExit = GUICtrlCreateButton("Exit", 275, 225, 85, 25) $idCloseFile = GUICtrlCreateButton("Close File", 157, 225, 85, 25) ; Run the psfile command to see who has files open in S:\everyone\Updated Monthly Schedules. Wait for command to complete and set variables as such. $command = RunAs("user", "Domain", "Password", 2, $useraccount & '\psfile.exe \\server "F:\share\Updated Monthly Schedules"', "", @SW_HIDE, $STDOUT_CHILD + $STDERR_CHILD) ProcessWaitClose($command) $display = StdoutRead($command) $arrPDFFiles = StringRegExp($display, '\[\d*\].*\.pdf', $STR_REGEXPARRAYGLOBALMATCH) ;Obtaining the list of PDF files $array = StringRegExp($display, '([^\\]+pdf)[^:]+:\h*(\N+)', 3) For $i = 0 to UBound($array)-2 step 2 _GUICtrlListBox_AddString($g_hListBox, $array[$i] & " -- " & $array[$i+1]) ;Create List box with data Next ; Display the GUI. GUISetState(@SW_SHOW, $hGUI) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ;When the 'X' at the top right is clicked, close the GUI ExitLoop Case $idExit ;When the 'Exit' button is clicked, close the GUI Exit Case $idQuery ;When the 'Refresh Query' button is clicked, delete the GUI and re-launch it to "refresh". GUIDelete($hGUI) FileQuery() Case $idCloseFile ;When the 'Close File' button is clicked, gather the selected information and put the full path back together. When done, run the close command then close the app and re-launch to "refresh". $selected = '' For $j = 0 To _GUICtrlListBox_GetCount($g_hListBox) - 1 If _GUICtrlListBox_GetSel($g_hListBox, $j) Then $strip = StringTrimLeft($arrPDFFiles[$j], StringInStr($arrPDFFiles[$j], "F") - 1) RunAs("user", "Domain", "Password", 2, @ComSpec & ' /c ' & $useraccount & '"\psfile.exe" \\server ' & '"' & $strip & '"' & ' -c') ;MsgBox("", "", @ComSpec & ' /c ' & $useraccount & '\psfile.exe" \\server ' & '"' & $strip & '"' & ' -c') GUIDelete($hGUI) FileQuery() EndIf Next EndSwitch WEnd ; Delete the previous GUIs, all controls, and temp file. GUIDelete($hGUI) EndFunc Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 10, 2019 Share Posted December 10, 2019 (edited) @tros804 Just add the (?i) modifier at the beginning of the SRE pattern: expandcollapse popup#include <File.au3> #include <FileConstants.au3> #include <StringConstants.au3> Global $strFileContent, _ ; Content of the File to read $arrPDFFiles, _ ; Array in which will be stored (if present) all the lines with .pdf and User in those $arrResult, _ ; Result of the SRE $strFileName = "", _ ; File Name (locked by some user) $strUsername = "" ; Username who locks the PDF file ; Reading the content of the file $strFileContent = FileRead(@ScriptDir & "\SampleText.txt") ; Obtaining the list of PDF files $arrPDFFiles = StringRegExp($strFileContent, '(?i)\[\d*\].*\.pdf\s*User:\s*\w*', $STR_REGEXPARRAYGLOBALMATCH) ; (?i) added here ; If there are some (PDFs) If IsArray($arrPDFFiles) Then For $i = 0 To UBound($arrPDFFiles) - 1 Step 1 ; Obtaining the full filename and user who is locking it $arrResult = StringRegExp($arrPDFFiles[$i], '(?i)\[\d*\](.*\.pdf)\s*User:\s*(\w*)', $STR_REGEXPARRAYMATCH) ; (?i) added here ; Obtaining only the name of the PDF $strFileName = _PathSplitEx($arrResult[0]) ; Obtain the user who is locking the PDF file $strUsername = $arrResult[1] ; Printing the result ConsoleWrite($i + 1 & ": '" & $strFileName & "' locked by '" & $strUsername & "'" & @CRLF) Next EndIf Func _PathSplitEx($strPath) Local $strDrive, _ $strDir, _ $strFileName, _ $strExtension If IsArray(_PathSplit($arrResult[0], $strDrive, $strDir, $strFileName, $strExtension)) Then Return $strFileName & $strExtension EndFunc SampleText.txt: Spoiler [10429393] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read ? [10429406] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019 User: STEPHENG Locks: 0 Access: Read [10429409] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10429413] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019 User: STEPHENG Locks: 0 Access: Read [10430671] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shift 2019\03March 2019\wal-mart supercenter 16.pdf User: STEPHENG Locks: 0 Access: Read ? [10430671] N:\SHARE\UPDATED MONTHLY SCHEDULES\MONTHLY SCHEDULES AND AVAILABLE SHIFT 2019\03MARCH 2019\WAL-MART SUPERCENTER 16 ALL MAIUSC.PDF User: STEPHENG Locks: 0 Access: Read ? [10568007] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017\somepdf.pdf User: ERICKT Locks: 0 Access: Read [10568013] N:\share\Updated Monthly Schedules\Monthly Schedules and Available Shifts 2017\June 2017 User: ERICKT Locks: 0 Access: Read Edited December 10, 2019 by FrancescoDiMuro Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
tros804 Posted December 10, 2019 Author Share Posted December 10, 2019 Thanks @FrancescoDiMuro. I ended up reading the help file on StringRegExp and found the (?i) argument shortly after posting the question to this post. Who would've thought the help file would be helpful?!?!?! Thanks again for responding. As far as I can tell, it's working as intended now. Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 10, 2019 Share Posted December 10, 2019 @tros804 Happy to have helped Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now