Sign in to follow this  
Followers 0
Nahuel

Rename All files in a directory.

9 posts in this topic

#1 ·  Posted (edited)

A guy who likes to have his pictures organized asked me for something to rename all the files in a folder (since he doesn't like names like "DCIM8796.JPG"). So I made this for him. Maybe someone can also find it useful.

Use at your own risk.

#include <File.au3>
#include <Constants.au3>
#include <Guiconstants.au3>
#Include <GuiListView.au3>
#NoTrayIcon
Opt("GUIOnEventMode",1)

While 1
$Directorio=FileSelectFolder("",@HomePath) 
If @error=1 Then
    MsgBox(0,"Error","No folder selected.")
    Exit
EndIf

$lista1=_FileListToArray($Directorio,"*.*",1)

If $lista1="" OR $Directorio=@WindowsDir OR $Directorio=@ProgramFilesDir Then
    MsgBox(48,"Error","The selected folder is either empty or not valid.")
Else
    ExitLoop
EndIf
WEnd

$alto=(25 * $lista1[0]+30)
If $alto > @DesktopHeight Then
    $altoventana=300
    $altolista=250
Else
    $altolista=$alto
    $altoventana=$alto+50
EndIf

GUICreate("Archivos",250,$altoventana)
GUISetOnEvent($GUI_EVENT_CLOSE,"_exit")
$ListaArchivos=GUICtrlCreateListView("Files in "&$Directorio,10,10,230,$altolista,$LVS_NOSORTHEADER,$LVS_EX_CHECKBOXES)
$renombrar=GUICtrlCreateButton("Rename Selected Files",10,$altoventana-30,230)
GUICtrlSetOnEvent(-1,"cambiar_nombre")
GUISetState()

obtenerArchivos()

Func obtenerArchivos()
$iCheckboxPos = 15
    Global $aCheckbox[UBound($lista1)]
    For $i = 1 To $lista1[0] Step 1
        $aCheckbox[$i] = GUICtrlCreateListViewItem($lista1[$i],$ListaArchivos)
        GUICtrlSetState(-1, $GUI_CHECKED)
        $aCheckbox[0] = UBound($aCheckbox)
    Next
EndFunc

Func _exit()
    Exit
EndFunc

Func cambiar_nombre()
    $nuevonombre=InputBox("Rename Files","Please type the new name for the selected files. A number will be added at the end of each filename.")
    If $nuevonombre<>"" Then
    Dim $iMsgBoxAnswer
    $iMsgBoxAnswer = MsgBox(52,"Warning!","You are about to rename ALL the selected files." & @CRLF & "Continue?")
    Select
       Case $iMsgBoxAnswer = 6 ;Yes
        $N=1
        For $i = 1 To (UBound($aCheckbox)-1)
            $sActiveCheckbox = $aCheckbox[$i]
            If _GUICtrlListViewGetCheckedState($ListaArchivos,$i-1)  Then
                $ArchivoArenombrar=GUICtrlRead($aCheckbox[$i])
                $EXTENSION=StringRight($ArchivoArenombrar,4)
                $largo=StringLen($ArchivoArenombrar)
                $NOMBRE=(StringLeft($ArchivoArenombrar,$largo-4))
                $MoverError=FileMove($Directorio&"\"&$ArchivoArenombrar,$Directorio&"\"&$nuevonombre&"("&$N&")"&$EXTENSION)
                If $MoverError=0 Then
                    MsgBox(48,"Error","Could not rename: " &$ArchivoArenombrar& @CRLF & "The file is read-only, you don't have permission or the new name matches the old one.") 
                EndIf
                $N=$N+1
            EndIf
        Next
        MsgBox(64,"Files Renamed","Finished!")
        Exit
       Case $iMsgBoxAnswer = 7 ;No
       EndSelect
    EndIf

EndFunc;==>cambiar_nombre()

While 1
    Sleep(100)
WEnd
Edited by Nahuel

Share this post


Link to post
Share on other sites



I made something like for my GIMP brushes... I was sick of having all these people's weird names for the brush file names... I didn't think of using this for pictures though.. Good idea!


My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

Share this post


Link to post
Share on other sites

Is your friend not running Windows XP? Cus with XP if you just select all the files, then hit F2, it will rename them all in a very similar fashion. Cool script though, well done. I like the use of the listview to select which files to rename.

Share this post


Link to post
Share on other sites

WinXP HAS THAT!? :)

And thanks guys, it took me quite a while to figure out how to know which ones are checked, hehe.

Share this post


Link to post
Share on other sites

thanks nahuel...greatz example :)


AUTOIT[sup] I'm lovin' it![/sup]

Share this post


Link to post
Share on other sites

[autoit]; language: Romana/Engleza

; platform: 98/Me/win2k/xp

; author: Neagu Ionut (aka SmLex)

; email: smlex2006@yahoo.com

; Date: 5 Ianuarie 2007

#Region Compiler directives section

#Compiler_Prompt = n ;y=show compile menu

;** AUT2EXE setting

;#Compiler_AUT2EXE = C:\Program Files\AutoIt3\beta\Aut2exe\aut2exe.exe ;Override the default compiler with this version.

#Compiler_Icon = iconita.ico ;filename of the Ico file to use

#Compiler_Compression = 4 ;Compression parameter 0-4 0=Low 2=normal 4=High

#Compiler_Allow_Decompile = y ;y= allow decompile

#Compiler_PassPhrase=19861987 ;Password to use for compilation

;** Target program Resource info

#Compiler_Res_Comment = Pentru mai multe informatii despre Redenumeste SL vizitati www.softwaresl.evonet.ro

#Compiler_Res_Description = Program de redenumit mai multe fisiere in acelasi timp.

#Compiler_Res_Fileversion = 2.0 ;File Version

#Compiler_Res_FileVersion_AutoIncrement=n

#Compiler_Res_LegalCopyright=

Share this post


Link to post
Share on other sites

^ what the hell is that? :)

Share this post


Link to post
Share on other sites

#include "WinAnimate.au3"

===

But where's "WinAnimate.au3"?

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  
Followers 0