Jump to content

Help putting script together: find files and folders


Recommended Posts

Hi there,

I'm still very much a beginner with AutoIT. I've been doing loads of searches on this forum and reading the help file, but I'm really struggling to get very far. I've been trying to piece together a script from various items I have found on this (amazing:-) forum.

To explain what I'm trying to do: search through some folders looking for text files called version.txt. I then need to look in the version.txt files to find if they contain some text. If they don't contain the text I'm looking for, then I need the containing folder tree deleted (for that particular version.txt).

Here's what I have managed to piece together so far.

This bit searches the folders and finds the version.txt files and their location:

Search ("C:\Drivers","version.txt");replace with your search directory and file required

Func Search($current,$toFind)
    If StringRight($current,1) = "\" then $current = StringTrimRight($current,1)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

           ;IF $file = $toFind then Msgbox(0,"File found", $current & "\" & $file); you could add it to an array or whatever else you wanted to do with it
            IF $file = $toFind then Msgbox(0,"File found", $current); you could add it to an array or whatever else you wanted to do with it
                
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $toFind)
            
        EndIf
        
    WEnd
    FileClose($search)

EndFunc

I've also got this bit of code that reads the version.txt looking for the text I am interested in:

;Open File for reading
$file = FileOpen("C:\Drivers\R174191\version.txt", 0); Open in Read mode
$sReadLines = ""

;Main Script

While 1
    $sReadLines = FileReadLine($file)
    If @error <> 0 Then ExitLoop; Exit if EOF or unable to read file
    If StringInStr($sReadLines, "XT") Then
    ;Do something...
        MsgBox(0, "Line info", $sReadLines)
    EndIf
WEnd

FileClose($file)

But I don't know how to put the two bits together? Also, how would I go about removing the folder trees that don't contain a version.txt with the interesting data?

Any help or advice from one of you gurus out there will be greatly received ;-)

Thanks in advance.

Kind regards,

Greg.

Link to comment
Share on other sites

Hi there,

I'm still very much a beginner with AutoIT. I've been doing loads of searches on this forum and reading the help file, but I'm really struggling to get very far. I've been trying to piece together a script from various items I have found on this (amazing:-) forum.

To explain what I'm trying to do: search through some folders looking for text files called version.txt. I then need to look in the version.txt files to find if they contain some text. If they don't contain the text I'm looking for, then I need the containing folder tree deleted (for that particular version.txt).

Here's what I have managed to piece together so far.

This bit searches the folders and finds the version.txt files and their location:

Search ("C:\Drivers","version.txt");replace with your search directory and file required

Func Search($current,$toFind)
    If StringRight($current,1) = "\" then $current = StringTrimRight($current,1)
    Local $search = FileFindFirstFile($current & "\*.*")
    While 1
        Dim $file = FileFindNextFile($search)
        If @error Or StringLen($file) < 1 Then ExitLoop
        If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then

          ;IF $file = $toFind then Msgbox(0,"File found", $current & "\" & $file); you could add it to an array or whatever else you wanted to do with it
            IF $file = $toFind then Msgbox(0,"File found", $current); you could add it to an array or whatever else you wanted to do with it
                
        EndIf
        If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
            Search($current & "\" & $file, $toFind)
            
        EndIf
        
    WEnd
    FileClose($search)

EndFunc

I've also got this bit of code that reads the version.txt looking for the text I am interested in:

;Open File for reading
$file = FileOpen("C:\Drivers\R174191\version.txt", 0); Open in Read mode
$sReadLines = ""

;Main Script

While 1
    $sReadLines = FileReadLine($file)
    If @error <> 0 Then ExitLoop; Exit if EOF or unable to read file
    If StringInStr($sReadLines, "XT") Then
;Do something...
        MsgBox(0, "Line info", $sReadLines)
    EndIf
WEnd

FileClose($file)

But I don't know how to put the two bits together? Also, how would I go about removing the folder trees that don't contain a version.txt with the interesting data?

Any help or advice from one of you gurus out there will be greatly received ;-)

Thanks in advance.

Kind regards,

Greg.

Why does your recursive Search() function find *.* for the first file, when you are looking for "version.txt"?

Also, this logic fails: '($file <> "." Or $file <> "..")' It will always return true because no string can be both "." and ".." at the same time, so it will always not match one or the other. It is also unnecessary, because I don't think FileFindNextFile() ever returns those meta-file names.

In addition, you can't start deleting "trees" without being very specific about what conditions will trigger deletion at what level in the "tree". You recursive call to Search() needs a way to know if it is the root of the part to be deleted or not. That means the top (first search function) should probably be coded differently, because it should behave differently than the child search on fail, or the single Search() function needs a flag to pass in parameters to know if it is the top instance or not.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Hi PsaltyDS,

Thanks for the swift response ;-)

I'm not sure I understand how to fix the errors you've mentioned? I did just find the script elsewhere on the forum, and made a minor change to it and I thought it was working OK?? Shows what I know...

For the setting of the root dir thing, should I just enter something like If $current = "C:\Drivers" Then do something??

Sorry to sound dumb, but I'm struggling here...

Thanks,

Greg.

Link to comment
Share on other sites

Hi PsaltyDS,

Thanks for the swift response ;-)

I'm not sure I understand how to fix the errors you've mentioned? I did just find the script elsewhere on the forum, and made a minor change to it and I thought it was working OK?? Shows what I know...

For the setting of the root dir thing, should I just enter something like If $current = "C:\Drivers" Then do something??

Sorry to sound dumb, but I'm struggling here...

Thanks,

Greg.

Well, I was wrong about the "*.*", because to do a recursive search you have to find all the subdirectories in addition to matching files. So "*.*" was correct there. But the rest of that Search() function looks badly broken. For example it doesn't appear to return anything. Search the forum for recursive searches. There are several working ones posted.

For the other part, you should get the search right first, then come back to that. Get your script to return an accurate list of all the version.txt files with full paths. After you get that, decide how to handle the list.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...