Sign in to follow this  
Followers 0
amanda089

Directory Recursion?

6 posts in this topic

#1 ·  Posted (edited)

How would I recurse through the @TempDir returning only the directory found... like $search=FileFindFirstFile("filename.ext") and FileFindNextFile ($search)

I want to make a progressbar for emptying the Temp directory

EDIT: had a brain-fart, forgot about the little button called Search..

Edited by amanda089

Share this post


Link to post
Share on other sites



Hmm... It looks like I will need help after all..

I got this code from a post:

#include <file.au3>

Global $logfile = @ScriptDir & "\log.txt"

_FileWriteLog($logfile,"Log Opened")
SearchDir()
_FileWriteLog($logfile,"Log Closed")
exit

Func SearchDir($dir = ".")
    local $file, $search

    $search = FileFindFirstFile($dir & "\*.*")  
    if $search = -1 then return
    
    while 1
        $file   = FileFindNextFile($search) 
        If @error Then ExitLoop
        if $file <> "." and $file <> ".." Then
            if IsDirectory($file) Then
                SearchDir($dir & "\" & $file)
            Else
                RenameAndLog("ABC","XYZ",$dir,$file)
            Endif
        Endif
    WEnd
    FileClose($search)
EndFunc

Func RenameAndLog($old,$new,$dir,$file)
    local $newfile = $file, $ret

    $newfile = StringReplace($file,$old,$new,0,1)
    if $newfile <> $file Then
        if FileMove($dir & "\" & $file,$dir & "\" & $newfile) then
            _FileWriteLog($logfile,$dir & "\" & $file & " Renamed to " & $dir & "\" & $newfile)
        Else
            _FileWriteLog($logfile,"Failed to rename " & $dir & "\" & $file)
        Endif
    Endif
EndFunc

Func IsDirectory($file)
    if DirGetSize($file,2) = -1 Then return 0
    return 1
EndFunc

And it seems that there is no IsDirectory($Directory) function.

which means that I am back to the drawing board

Share this post


Link to post
Share on other sites

Hmm... It looks like I will need help after all..

And it seems that there is no IsDirectory($Directory) function.

which means that I am back to the drawing board

<{POST_SNAPBACK}>

Don't understand.... last Func in your posted script is called IsDirectory() .. right ???

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

hint:

Returns the size of a file in bytes.

FileGetSize ( "filename" )

DirGetSize

--------------------------------------------------------------------------------

Returns the size in bytes of a given directory.

this is a UDF User Defined Funtion.

works the same as a built in one pretty much.

Func IsDirectory($file)
    if DirGetSize($file,2) = -1 Then return 0
    return 1
EndFunc
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

LOL... either my eyes are going bad, or I shouldn't try to write any scripts while half-asleep

Share this post


Link to post
Share on other sites

.. or ..

You can use DOS's built-in recursion:

;let's build an array of subfolders
    $aFolder = _GetFileList("C:\TEMP", "/AD")
   ;msgBox(0,"Number of folders",$aFolder[0])

Func _GetFileList($psFolder, $psSwitch)
;---------------------------------------------------------------------------------------------------
;$psSwitch can be the following kinds of things:
;   /AD     list folders only
;   /A-D       list files only
;   /S /AD   list all folders and subfolders recursively
;   /A-D /OE   list only files, and group them by extension
;
; <<< parameters to the DIR command >>>
;
;   /A        Displays files with specified attributes.
;   attributes   D  Directories             R  Read-only files
;                H  Hidden files               A  Files ready for archiving
;                S  System files               -  Prefix meaning not
;   /O        List by files in sorted order.
;   sortorder   N  By name (alphabetic)    S  By size (smallest first)
;                E  By extension (alphabetic)  D  By date & time (earliest first)
;                G  Group directories first -  Prefix to reverse order
;   /T        Controls which time field displayed or used for sorting
;   timefield   C  Creation
;               A  Last Access
;               W  Last Written
;   /S        Displays files in specified directory and all subdirectories.
;   /X        This displays the short names generated for non-8dot3 file
;               names.  The format is that of /N with the short name inserted
;               before the long name. If no short name is present, blanks are
;               displayed in its place.
;---------------------------------------------------------------------------------------------------
            
   ;decide on a temp file
    $sTmp = @TempDir & "\~getlist.lst"
    
   ;Reroute a DIR command to the temp file
    RunWait(@COMSPEC & " /c DIR /B " & $psSwitch & " """ & $psFolder & """>""" & $sTmp & """", "", @SW_HIDE)
    
   ;Read the textfile into an array
    $sFileList = FileRead($sTmp, FileGetSize($sTmp))
    
   ;Cleanup
    FileDelete($sTmp)

   ;And leave
    Return StringSplit(StringReplace($sFileList, @CRLF, @LF),@LF)

EndFunc

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