Jump to content

Pull line data from text file and subsequent line


Recommended Posts

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.

#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

The following assumes that there can be several pdf/user couples present in the .txt file

#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 by mikell
Link to comment
Share on other sites

Hi @tros804, and welcome to the AutoIt forums :welcome:
You can use something like this:

#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 by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

  • 9 months later...

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.

image.thumb.png.0975831926998f9dfd91ad1f68766842.png

 

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...

#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

@tros804
Just add the (?i) modifier at the beginning of the SRE pattern:

#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 by FrancescoDiMuro

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...