Sign in to follow this  
Followers 0
Garrett19212

File Search

16 posts in this topic

How can I make it so it searches the whole computer for a file? And if it finds it, it deletes it?

I tried FileFindFirstFile but I can't get it to work right.

Share this post


Link to post
Share on other sites



Search in Scripts and Scraps, there's a few FileSearch UDF's there that should do the job.


SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

bump^

Share this post


Link to post
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()?

Share this post


Link to post
Share on other sites

what would i put for it though, since the file isnt in the folder where the script?

Ill try FileDelete($file) and see what happens.

Share this post


Link to post
Share on other sites

Nope, i made it search for a file called 4096, and i made a file called 4096 and put it on my desktop and it didn't delete it.

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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)

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
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
Sign in to follow this  
Followers 0