Jump to content

File Search


Recommended Posts

I couldn't find anything that looks like that I need, or atleast it doesn't look what I need.

Im new to AU3 so still learning what everything does.

I have a txt file, with a filename on each line.

And I have a loop which sets the next line to a variable, which the FileFindFirstFile set to search for the variable. But its not working, i made a file with the exact name of what I want it so look for, but it doesn't find it.

How can I make it search the whole C: drive, for the file?

Link to comment
Share on other sites

You could do something like this (I'm not at home and cannot test this, so bear in mind I may have written a function name wrong or something):

$resultFile = 'c:\SearchResults.txt'
$file = 'stupidfile.exe'
RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)
If FileSize($resultFile) = 0 Then
   MsgBox(0, 'File Not Found', 'File Not Found')
Else
   $searchResults = FileRead($resultFile, FileSize($resultFile))
EndIf

Give it a try.

Link to comment
Share on other sites

Seems to work, changed filesize to filegetsize and im not getting any errors so.

How would I make it so that if it finds something it deletes it?

<{POST_SNAPBACK}>

Have you tried FileDelete()?
Link to comment
Share on other sites

I added the following to the script and it appears to work:

$file = FileOpen($resultFile, 0)
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    FileDelete($line)
Wend

FileClose($file)
;FileDelete($resultFile)
Not All Who Wander Are Lost
Link to comment
Share on other sites

$resultFile = 'c:\SearchResults.txt'

$file = 'stupidfile.exe'

RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)

If FileSize($resultFile) = 0 Then

MsgBox(0, 'File Not Found', 'File Not Found')

Else

$searchResults = FileRead($resultFile, FileSize($resultFile))

EndIf

$file = FileOpen($resultFile, 0)

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

FileDelete($line)

Wend

FileClose($file)

;FileDelete($resultFile)

Like that?

Is the ; suppose to be by the filedelete

Link to comment
Share on other sites

You could do something like this (I'm not at home and cannot test this, so bear in mind I may have written a function name wrong or something):

$resultFile = 'c:\SearchResults.txt'
$file = 'stupidfile.exe'
RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)
If FileSize($resultFile) = 0 Then
   MsgBox(0, 'File Not Found', 'File Not Found')
Else
   $searchResults = FileRead($resultFile, FileSize($resultFile))
EndIf

Give it a try.

<{POST_SNAPBACK}>

You probably want to start the search at the root of the drive:

RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)
Link to comment
Share on other sites

Since you're just shelling out to run the DIR command, why even bother with autoit? Why not just write a batch file?

RecursiveDelete.Bat:

FOR /F "tokens=*" %%A in ('DIR "\%~1" /s/b') do DEL %%A

Then call it like this:

RecursiveDelete FileName.Ext

it will also work for wildcards.

EDIT: Oh, I forgot to mention - this method won't work on Windows 9x or WindowsNT 4 or earlier. I don't know about ME.

Edited by blindwig
Link to comment
Share on other sites

I would like to do it in Autoit.

$resultFile = 'c:\SearchResults.txt'

$file = 'stupidfile.exe'

RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)

If GetFileSize($resultFile) = 0 Then

MsgBox(0, 'File Not Found', 'File Not Found')

Else

$searchResults = FileRead($resultFile, GetFileSize($resultFile))

EndIf

How would I make that, delete the files it finds?

Link to comment
Share on other sites

Like that?

Is the ; suppose to be by the filedelete

<{POST_SNAPBACK}>

That last ; I put in just in case you did not want to delete the 'SearchResults.txt' file when you are done ... I did on mine as not to leave any files behind when done running the script. Edited by Brona
Not All Who Wander Are Lost
Link to comment
Share on other sites

I would like to do it in Autoit.

Then take out the RunWait line and use FileFindFirstFile. Look at the example in the help file, it shows you exactly how to loop through the files, matched by a mask.

$resultFile = 'c:\SearchResults.txt'

$file = 'stupidfile.exe'

RunWait(@ComSpec & ' /c dir "' & $file & '" /b/s > "' & $resultFile & '"', 'C:\', @SW_HIDE)

If GetFileSize($resultFile) = 0 Then

  MsgBox(0, 'File Not Found', 'File Not Found')

Else

  $searchResults = FileRead($resultFile, GetFileSize($resultFile))

EndIf

How would I make that, delete the files it finds?

The files that it finds will be listed in the $resultFile, so go through that file and delete each file entry. Use FileOpen, FileReadLine, and FileClose, to use the file.
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...