Jump to content

Recursive File Find & Rename


Recommended Posts

Group,

Could use a little assistance or advice with a problem at work. Our company is in the process of merging internal divisions, and as such want to standardize and eliminate references to the old divisions. In the past we have used 3 digit identifiers for each division as a way to tell at a glance what division a document or file is referring to. I would like to be able to create an Autoit script that would do a recursive search through a directory and replace a specific 3 digit string in a file name with a new 3 digit string, example:

ABC Financials.xls would become XYZ Financials.xls

For the sake of being able to rename the file back if necessary due to a link from somewhere else, it would be nice to have a log file created that says:

Old file= ABC Financials.xls New file= XYZ Financials.xls and the folder the file was located in.

I'm familiar with doing simple search and replaces for strings in a file name, but not with the logic of a recursive search through an entire directory structure, then piping the files to a log file.

As always, thanks in advance...

ZK

Link to comment
Share on other sites

If its always the first 3 characters you might want to use

If StringLeft($file,3) = $old Then ...

#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
Edited by Ejoc
Start -> Programs -> AutoIt v3 -> AutoIt Help File -> Index -> (The Function you are asking about)----- Links -----DllStruct UDFsRSA Crypto UDFs
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...