Jump to content

My @comSpec findstr function does not work


 Share

Recommended Posts

I'm trying to use "findstr" to search a number of .mxliff files (think of them as text files) for the phrase "RK manufacturer" and display the names of the matched files in MsgBox. But all I get is an empty file.

 

Run(@ComSpec & " /c findstr RK manufacturer \C:\Users\na\Downloads\*.mxliff > C:\Users\na\Downloads\temp.txt")

$strFileContent = FileRead("C:\Users\na\Downloads\temp.txt")

MsgBox(0,"Files Matched", $strFileContent)

 

Where did I get it wrong in the syntax? Also, when should I use RunWait() instead. THANKS.

 

 

ss

Edited by kcston
Link to comment
Share on other sites

Your findstr seems incorrect.  Make sure you use the correct syntax by going in a DOS console and make it work there first.  You should use RunWait as it will wait for the result to be completed before going further.  Grab the exit code so you know if there is an error in your DOS statement.  Put some error handling (checking @error after each statement) in order to find if something goes wrong.

FYI. Currently, even if your findstr was correct, Run will not wait for completion of the DOS statement, so the result will always be empty at the time you read the file...

 

Link to comment
Share on other sites

Thanks for the quick comments. I'm still new to AutoIt so please bear with me.

 

For testing, I have created the following two files in the \Downloads directory.

1.mxliff (1-line content: <target>RK manufacturer available</target>)
2.mxliff (1-line content: <target>RK manufacturer not available</target>)

 

At the command prompt of my Windows 11, the following works. 

findstr manufacturer C:\Users\na\Downloads\*.mxliff > C:\Users\na\Downloads\temp.txt

 

temp.txt is produced and it contains the following content:

C:\Users\na\Downloads\1.mxliff:<target>RK manufacturer available</target>
C:\Users\na\Downloads\2.mxliff:<target>RK manufacturer not available</target>

 

However, when put into AutoIt, the following code does not seem to work. No temp.txt is produced and no error detected.  Msgbox shows empty content.

RunWait(@ComSpec & " findstr manufacturer C:\Users\na\Downloads\*.mxliff > C:\Users\na\Downloads\temp.txt")

If @error Then
    MsgBox(0,"Error",@error)
Else
    $strFileContent = FileRead("C:\Users\na\Downloads\temp.txt")
    MsgBox(0,"File", $strFileContent)
EndIf

What else can I try?

 

Edited by kcston
Link to comment
Share on other sites

Here the way with adding error handling :

Local $iExit = RunWait(@ComSpec & " /c findstr MsgBox C:\Apps\AutoIt\*.au3 > C:\Apps\Temp\temp.txt")
If @error Or $iExit Then Exit MsgBox(0,"Error RunWait",@error & "/" & $iExit)
Local $strFileContent = FileRead("C:\Apps\Temp\temp.txt")
If @error Then Exit MsgBox(0,"Error FileRead",@error)
MsgBox(0,"File", $strFileContent)
FileDelete("C:\Apps\Temp\temp.txt")

 

Link to comment
Share on other sites

I poked around some and found this to work (assuming you're working out of your user profile):

#include <FileConstants.au3>

$sFilePath = @UserProfileDir & "\Downloads\temp.txt"

$iPID = Run(@ComSpec & ' /c findstr "manufacturer" ' & @UserProfileDir & '\Downloads\*.mxliff > ' & $sFilePath)
ProcessWaitClose ($iPID)

If @error Then
    MsgBox(0,"Error",@error)
Else
    $hFileOpen = FileOpen ($sFilePath , $FO_READ)
    $strFileContent = FileRead($hFileOpen)
    If @error Then
        MsgBox(0,"Error",@error)
        Exit
    EndIf
    FileClose($hFileOpen)

    MsgBox(0 ,"File" , $strFileContent)
EndIf

 

Link to comment
Share on other sites

Somehow already the way I forgot to include the /c switch before findstr. My final version shown below works fine.

RunWait(@ComSpec & " /c findstr /i /c:" &  Chr(34) & $strPhrase & Chr(34) & " " & Chr(34) & $strFile & Chr(34) & " > C:\Users\na\Downloads\temp.txt")

Thank you all for the help. Much appreciated.

 

 

Edited by kcston
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...