Jump to content

Can Anyone Help Me Refine My Script


Recommended Posts

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 ? :)

#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
Link to comment
Share on other sites

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 ? :)

#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
i 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
Link to comment
Share on other sites

  • Developers

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.
  :)

Link to comment
Share on other sites

Haven't tested but this doesn't look right:

if IsDirectory($file) Then

shouldn't that be?:

if IsDirectory($dir & "\" & $file) Then

Holy ! Yeah ! That did it !

Thanks a lot JdeB !!! :(:)

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...