Sign in to follow this  
Followers 0
ChrisL

Searching for Directories and Files within

24 posts in this topic

#1 ·  Posted (edited)

Help..

I need to find specific files which I can do.. but I would like to be able to check subdirectories too.. the subdirectory names are not known and can change.

Basically it looks for RAW image files and then calls one of our own applications to convert the raw file to a BMP and then puts them in a separate folder called BMP.

But I have to do this for each folder on its own I would like to do it from the next level up and search all folders..

My Code so far is....

#include <file.au3>

$search = FileFindFirstFile("*.RAW")  
$Count=0

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

While 1
    $file = FileFindNextFile($search) 
    If @error Then ExitLoop
    $Count = $Count + 1
    
    
WEnd

; Close the search handle
FileClose($search)


$qtyLeft = $Count


$search = FileFindFirstFile("*.RAW")  

; Check if the search was successful
If $search = -1 Then
    MsgBox(0, "Error", "No files/directories matched the search pattern")
    Exit
EndIf

    
$File1 = " .....Searching"
    
ProgressOn ( "Searching for files", "Please Wait" , "Found File" & $File1 )


While 1
    $file1 = FileFindNextFile($search) 
    If @error Then ExitLoop
    
    
    $Percent =  $qtyLeft * 100 / $Count
    $Percent1 = 100 - $Percent 

    ProgressSet ( $Percent1 , "Found File " & $File1 & "     Files Left to search " & $qtyLeft, "Please Wait"  )
    ControlSetText("TitleFoo", "", "Static1", "Checking File" & $File1)
    $QtyLeft = $QtyLeft - 1
    
    RunWait(@COMSPEC & " /c Raw2bmp.exe " & $File1, @SW_HIDE )
  

    
    
    
    Wend



ProgressSet ( 100 , "Found File " & $File1, "Completed"  )
Sleep (1000)
DirCreate ( "BMP" )
FileMove ( "*.BMP", "BMP" , 1 )

ProgressOff ( )
Edited by ChrisL

Share this post


Link to post
Share on other sites



Welcome to the forums!

You do not appear to specify a directory in your code. Are you currently working with files located in the same folder as the script and the converter?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Welcome to the forums!

You do not appear to specify a directory in your code. Are you currently working with files located in the same folder as the script and the converter?

<{POST_SNAPBACK}>

Yes I am..

But if I ran it from the Higher Directory level and got it to look for all sub directories then I wouldn't have to put it in every directory and run it. I cant specify a Directory name because it could be anything

Edit* Oh and Hello :whistle:

Edited by ChrisL

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Alright, so would it be right to say that ideally you want to target your script on a particular folder and then have that folder, its subdirectories and their subdirectories processed?

What I would first suggest is chunking the code down a bit into functions, because it will make the whole thing much easier (and in the case of folder recursion, doable).

If you were to make a function called ProcessFolder(), you could have it do the following steps in order:

  • find each .RAW file within the folder;
  • convert each .RAW to a .BMP;
  • create a BMP subfolder;
  • move all .BMPs to BMP;
  • list all subfolders;
  • run itself (from within itself) on each subfolder.
Something like this should get you started:

deprecated -- read on

Edit: must close search handles afterwards!

Edit: d'oh, better declare the variables' scopes.

Edit: incomplete filepath was used so no directory traversal.

Edited by LxP

Share this post


Link to post
Share on other sites

Mmmm.. it doesn't like the true or false bit

func isFolder($path)
    if stringInStr(fileGetAttrib($path), "D") then return true
    return false
endFunc

I get an error when the sript runs.. I'm getting confused with this :whistle:

Share this post


Link to post
Share on other sites

Maybe this will work better:

func isFolder($path)
    if stringInStr(fileGetAttrib($path), "D") then return 1
    return 0
endFunc

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Sorry -- that was dodgy coding on my part. Try the revised code above.

Share this post


Link to post
Share on other sites

Sorry -- that was dodgy coding on my part. Try the revised code above.

<{POST_SNAPBACK}>

thats OK :whistle:.. but the bad news is it still does it..

If I take your code without adding anything to it.. in theory it would just go through the directory structure but not actually do anything.. Yes?

It still comes up with the same error.

This dirctory stuff confuses me totally.. sorry

Share this post


Link to post
Share on other sites

Sorry, this is what happens when I rush through coding... Updated above.

At the moment all the code will do for each .RAW file is display its name in a message box (just added). You can pretty much copy the code from your original script into that spot with a few changes.

Here's something I didn't mention before: you will probably want to do this so that the script can find the raw2bmp utility regardless of the directory it's processing.

runWait(@scriptDir & '\Raw2bmp.exe "' & $path & "\" & $file & '"')

Or if the utility requires DOS 8.3 filenames:

$filepath = fileGetShortName($path & "\" & $file)
runWait(@scriptDir & "\Raw2bmp.exe " & $filepath)

Share this post


Link to post
Share on other sites

HI again..

there is still an issue.. I know this is not really fare to get you to work this out but I don't get it!!

It does not do the directory thing but sits in a loop in the first working DIR, when it tries to find the next directory it doesn't.. Does that make any sense?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Was missing a variable! Should work better now.

Edit: I really should start testing my code before I upload it to threads -- I must look like a disgrace sometimes...

Edited by LxP

Share this post


Link to post
Share on other sites

Was missing a variable! Should work better now.

Edit: I really should start testing my code before I upload it to threads -- I must look like a disgrace sometimes...

<{POST_SNAPBACK}>

No not likely mate.. I couldn't do it!!

Will try this :whistle:

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

I sat down and recoded it from the beginning, testing as I went along! :dance:

processFolder(@scriptDir)

func processFolder($path)

    local $fileHandle, $file, $dirHandle, $dir

    local const $FILESPEC = "*.raw"
; debug("Starting with " & $path)

; debug("Going to search for " & $path & "\" & $FILESPEC)
    $fileHandle = fileFindFirstFile($path & "\" & $FILESPEC)
    if ($fileHandle <> -1) then
        while (1)
            $file = fileFindNextFile($fileHandle)
            if (@error = 1) then exitLoop
            if ($file = "." or $file = "..") then continueLoop
            $file = $path & "\" & $file
            if (isFolder($file)) then continueLoop
            debug("Found file " & $file)
         ; here's the place to do any per-file manipulating
         ; $file contains the full path to the file
        wEnd
        fileClose($fileHandle)
     ; here's the spot to do processing on any folder that contained RAWs
     ; (such as making BMP subfolders and moving files)
    endIf

; debug("Going to search for subfolders")
    $dirHandle = fileFindFirstFile($path & "\*.*")
    if ($dirHandle <> -1) then
        while (1)
            $dir = fileFindNextFile($dirHandle)
            if (@error = 1) then exitLoop
            if ($dir = "." or $dir = "..") then continueLoop
            $dir = $path & "\" & $dir
            if (not(isFolder($dir))) then continueLoop
     ; debug("Found subfolder " & $dir)
            processFolder($dir)
        wEnd
        fileClose($dirHandle)
    endIf

endFunc

func isFolder($path)
    if stringInStr(fileGetAttrib($path), "D") then return 1
    return 0
endFunc

func debug($text)
    msgBox(0x40, "processFolder()", $text)
endFunc

Edit: added comments to the code.

Edit: should work with the AutoIt release version now.

Edit: copying and pasting is bad. Be careful! :whistle:

My main mistake before was assembling the code from within the forum page and not properly copying it to an editor for testing. This one works for me as expected -- how about you?

Edited by LxP

Share this post


Link to post
Share on other sites

I still get this

As the loop keeps going my $Path is C:\Program Files\AutoIT3\.\.\.\.\anneheheh.raw

the path just keeps increasing with .\

its like its not remembering where it got up to everytime it runs through and always goes back and finds the first dir of \. and because of this it never does any subfolders or exits..

Thanks for all of this :whistle:

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

I just remembered what the problem is -- it's to do with the current release of AutoIt (I was using the beta to code this solution). I should have picked that up from your last post but I was convinced that it was my evil code. :whistle:

I suggest grabbing a ZIP of the beta and overwriting your current release with this copy. The code above will then work as expected.

Edit: or as a (probably better) alternative, I'll add two or three lines to the above code to make it compatible. Just a sec.

Edit: Done now.

Edited by LxP

Share this post


Link to post
Share on other sites

Ahhh.. OK.. have downloaded it.

It worked up to a point and then I got $dirHandle = fileFindFirstFile ($Path & "\*.*")

Error unable to open file, the maximum number of open files has been exceeded

But deffinately progress :whistle:

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