Toppy Posted May 11, 2006 Posted May 11, 2006 Hi, I have the following script which renames files in current dir & subdirs. However I would like it the also rename in deeper directories. Like when I run it from D:\ so it also renames files in D:\dir1\dir12\dir123\dir1234 Can anyone help me refine it please ? expandcollapse popup#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(" - De Jaren '50","",$dir,$file) RenameAndLog(" - De Jaren '60","",$dir,$file) RenameAndLog(" - De Jaren '70","",$dir,$file) RenameAndLog(" - De Jaren '80","",$dir,$file) RenameAndLog(" - De Jaren '90","",$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
seandisanti Posted May 11, 2006 Posted May 11, 2006 Hi, I have the following script which renames files in current dir & subdirs. However I would like it the also rename in deeper directories. Like when I run it from D:\ so it also renames files in D:\dir1\dir12\dir123\dir1234 Can anyone help me refine it please ? expandcollapse popup#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(" - De Jaren '50","",$dir,$file) RenameAndLog(" - De Jaren '60","",$dir,$file) RenameAndLog(" - De Jaren '70","",$dir,$file) RenameAndLog(" - De Jaren '80","",$dir,$file) RenameAndLog(" - De Jaren '90","",$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 EndFunci wrote a script a while back that listed all of the files in a tree recursively. you could easily read in that list and then change the files listed... there are a few other solutions already made that you could easily tailor to your needs. do a search in scripts and scraps for recursive file search and you should find a post with 10-20 recursive solutions
Toppy Posted May 11, 2006 Author Posted May 11, 2006 Hi, I've searched and found some scripts about recursive searches but it looks all chinese to me, I have no clue how to implement it in the script above.
Developers Jos Posted May 11, 2006 Developers Posted May 11, 2006 Haven't tested but this doesn't look right: if IsDirectory($file) Then shouldn't that be?: if IsDirectory($dir & "\" & $file) Then SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Toppy Posted May 11, 2006 Author Posted May 11, 2006 Haven't tested but this doesn't look right:if IsDirectory($file) Thenshouldn't that be?:if IsDirectory($dir & "\" & $file) ThenHoly ! Yeah ! That did it ! Thanks a lot JdeB !!!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now