Jump to content

Need help - rename files


Recommended Posts

hello to all,

made small autoit renamer utlity that run from command line,

but can't resolve a problem.

i've 3 files :

goof y.jpg

g oofy.jpg

go o fy.jpg

my tool remove spaces, but want to obtain :

goofy.jpg

goofy_02.jpg

goofy_03.jpg

need idea to solve this issue.

thank you all,

m.

Link to comment
Share on other sites

Try this:

#include <File.au3>
$DIR = FileSelectFolder("SELECT","")
$COUNT = 1
$SEARCH = InputBox("SEARCH","File to search","goofy.jpg")
$FILE = _FileListToArray($DIR,"*" & StringRight($SEARCH,4),1)
If IsArray($FILE) Then
For $INDEX = 1 To $FILE[0]
    $NAME = StringStripWS($FILE[$INDEX],8)
    If $SEARCH = $NAME Then
        RunWait(@ComSpec & ' /C REN "' & $DIR & '\' & $FILE[$INDEX] & '" "' & $DIR & '\' & StringTrimRight($SEARCH,4) & '_' & $COUNT & StringRight($SEARCH,4)&'"',$DIR, @SW_HIDE)
        $COUNT += 1
    EndIf
Next
EndIf

*Not tested

When the words fail... music speaks.

Link to comment
Share on other sites

badguy,

it's a command line script that rename file in given directory.

as command line script you MUST compile it with :

Aut2exe.exe /in badguy.au3 /out badguy.exe /console /unicode

Syntax :

badguy c:\yourpath\*.jpg

if not exist .ini, .exe create badguy.ini whit follow structure :

z;r

g;z

first letter (es: z) is what you want to change

second letter (es: r) is what first letter change to

gozzo.doc -> zorro.doc

Several controls are inserted to avoid error in command line,

so code appear a bit 'dirty'.

thank you all for help,

m.

#include <file.au3>
#include <array.au3>
Dim $sourcedir
Dim $szDrive, $szDir, $szFName, $szExt



If $CmdLine[0] <> 1 Then 
    ConsoleWrite(@CRLF)
    ConsoleWrite("Right syntax : " & @ScriptName & " \sourcedir\*.xyz" & @CRLF)
    ConsoleWrite(@CRLF)
    
    If FileExists(@ScriptDir & "\badguy.ini") Then
;do nothing
    Else
        Run(@ComSpec & " /c " & "echo yourbadchar;#> " & @ScriptDir & "\badguy.ini")
    EndIf
        
    exit
ElseIf $CmdLine[0] = 1 Then
    
;//////////////////////////////////////////////////////////////////
;//        VARIABLES
;//////////////////////////////////////////////////////////////////
; Shows the filenames of all files in the current directory.
    $search_source_file = FileFindFirstFile($CmdLine[1])  
    $sourcedir = $CmdLine[1]
    $my_count = 1
;//////////////////////////////////////////////////////////////////
;//        CONTROLS 
;//////////////////////////////////////////////////////////////////
; Check if the search was successful
    If $search_source_file = -1 Then
        ConsoleWrite("No files/directories matched the search pattern" & @CRLF)
        Exit
    EndIf

    if stringleft(Stringright($sourcedir,4),1) <> "." Then
        ConsoleWrite("Right syntax : " & @ScriptName & " \sourcedir\*.xyz" & @CRLF)
        exit
    EndIf


;//////////////////////////////////////////////////////////////////
;//       SPLIT PATH FOR EASY USE
;//////////////////////////////////////////////////////////////////
    $split_source_Path = _PathSplit($sourcedir, $szDrive, $szDir, $szFName, $szExt)

;//////////////////////////////////////////////////////////////////
;//       MAIN
;//////////////////////////////////////////////////////////////////
    While 1
        $file = FileFindNextFile($search_source_file) 
        If @error Then ExitLoop

        
;----------------------------------------------------------------------------------------------
;remove extension from file name
        $new_file = StringReplace($file, $split_source_Path[4], "")
        
        If FileExists(@ScriptDir & "\badguy.ini") Then
;open file .ini to identify bad chars
            $file_ini = FileOpen(@ScriptDir & "\badguy.ini", 0)
        Else
            
            Run(@ComSpec & " /c " & "echo yourbadchar;#> " & @ScriptDir & "\badguy.ini")
;FileInstall("c:\badguy.ini", @WorkingDir & "\badguy.ini", 1)
            Sleep(1000)
            $file_ini = FileOpen(@ScriptDir & "\badguy.ini", 0)
        EndIf
            
            
; Check if file opened for reading OK
        If $file_ini = -1 Then
            ConsoleWrite(@CRLF)
            ConsoleWrite("Unable to open .ini !" & @CRLF)
            ConsoleWrite(@CRLF)
            Exit
        EndIf
;----------------------------------------------------------------------------------------------
;Read in lines of text until the EOF is reached
        While 1
            $line = FileReadLine($file_ini)
            If @error = -1 Then ExitLoop

;identify par1 e par2
            $split_line = StringSplit($line, ";")
            
;replace in filename bad char
            $new_file = StringReplace($new_file, $split_line[1], $split_line[2])
        Wend
        FileClose($file_ini)

;----------------------------------------------------------------------------------------------
;rename file
        if $file <> $new_file & $split_source_Path[4] Then
            if FileExists($split_source_Path[1] & $split_source_Path[2] & $new_file & $split_source_Path[4]) Then
                
                
                
;~              $my_count = $my_count + 1
;~              FileMove($split_source_Path[1] & $split_source_Path[2] & $file, $split_source_Path[1] & $split_source_Path[2] & $new_file & "_" & string($my_count) & $split_source_Path[4])
;~              ConsoleWrite("ren " & $file & " -> " & $new_file & "_" & string($my_count) & $split_source_Path[4] & @CRLF)
;~              Sleep(150)




            else
                FileMove($split_source_Path[1] & $split_source_Path[2] & $file, $split_source_Path[1] & $split_source_Path[2] & $new_file & $split_source_Path[4])
                ConsoleWrite("ren " & $file & " -> " & $new_file & $split_source_Path[4] & @CRLF)
                Sleep(150)
            
            EndIf
        EndIf
        
        
        
        $my_count = 1
    WEnd

; Close the search handle
    FileClose($search_source_file)
    
EndIf

ConsoleWrite(@CRLF)
ConsoleWrite("byebye!" & @CRLF)
ConsoleWrite(@CRLF)
Edited by myspacee
Link to comment
Share on other sites

- rename with progressive number on end of file name, solved !

go ofy.jpg -> goofy.jpg

g o o f y.jpg -> goofy_1.jpg

g o o f y.jpg -> goofy_2.jpg

- add ability to rename files in directory as progressive numbers (switch used) :

goofy.jpg -> 000001.jpg

my holidays.jpg -> 000002.jpg

your mad dog.jpg -> 000003.jpg

...

post code (to clean)

m.

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