Jump to content
Sign in to follow this  
Toppy

Can Anyone Help Me Refine My Script

Recommended Posts

Toppy

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

Share this post


Link to post
Share on other sites
seandisanti

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

Share this post


Link to post
Share on other sites
Toppy

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

Share this post


Link to post
Share on other sites
Jos

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

if IsDirectory($file) Then

shouldn't that be?:

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


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

Share this post


Link to post
Share on other sites
Toppy

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 !!! :(:)

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  

×