Jump to content
Sign in to follow this  
Diana (Cda)

Rename images in folder incrementally?

Recommended Posts

Diana (Cda)

If I have a folder with a bunch of files labelled, i.e., images_001.png to images_404.png, is there an easy way to rename them incrementally?

The reason is, I have an Excel workbook that inserts these images into a worksheet but it seems to only work if the files are numbered properly.  I didn't like some of the images so I deleted a total of about 10 so far and the macro didn't like that and wouldn't work.

I renamed them using a renamer app but I'd prefer an AutoIt script to do this.  Much less hassle and I can keep it right in the images folder.

Also, would there be a way to request of user which starting number to use?  The default would be "001", of course, but I just ran into the problem that after quite a while of searching for images to complete the total # I need, that Firefox downloads them starting with 001.  I currently need, as an example, for the new image files to start at "images_115.jpg".

Thanks!

Edited by Diana (Cda)

Share this post


Link to post
Share on other sites
PhoenixXL

Check this out

#include <File.au3>
#include <Array.au3>

;Rename every mp3 and mp4 file to "Prefix_15", "Prefix_16", ....so on file.
$aErr = Rename_Files("C:\Users\abhishek\Desktop\Presentation\Videos- 2 b cut-", "*.mp4|*.mp3|*.avi", "Prefix_", 15)
_ArrayDisplay( $aErr, "Error Occurred at")

; #FUNCTION# ====================================================================================================================
; Name ..........: Rename_Files
; Description ...: Rename files to some integer based on their extension.
; Syntax ........: Rename_Files($S_Folder, $S_Extension_IncludeMask[, $S_Prefix = ""[, $i_Start = 1]])
; Parameters ....: $S_Folder                - The Folder where the files are to be renamed.
;                  $S_Extension_IncludeMask - The extension of files to be renamed.
;                  $S_Prefix                - [optional] Prefix if any to be included in the filename. Default is "".
;                  $i_Start                 - [optional] The starting file index. Default is 1.
; Return values .: Success : Sets @extended to True. An array of filenames is returned that were not renamed - this is "" if all
;                            success.
;                  Failure : Sets @extended to False. No files were renamed.
; Author ........: Phoenix XL
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/150817-rename-images-in-folder-incrementally/
; Example .......: Yes
; ===============================================================================================================================
Func Rename_Files($S_Folder, $S_Extension_IncludeMask, $S_Prefix = "", $i_Start = 1, $iOverWrite = 1)

    If StringRegExp($S_Folder, "\\\z") = 0 Then $S_Folder &= "\"

    Local $aFiles = _FileListToArray($S_Folder, "*", 1), $aErr[1]
    If @error Then Return SetError(@error, @extended, -1)

    $aFiles_Index = _ArrayFindAllEx($aFiles, $S_Extension_IncludeMask, False)
    For $i = 0 To @extended - 1

        ;$aFiles[$aFiles_Index[$i]] is the filename that has its extension matched with the IncludeMask

        ;Rename the files that matched the extension.
        If FileMove($S_Folder & $aFiles[$aFiles_Index[$i]], $S_Folder & $S_Prefix & StringFormat("%03d", $i_Start) & Get_Extension($aFiles[$aFiles_Index[$i]]), $iOverWrite) = 0 Then

            ConsoleWrite("!Failed:" & $S_Folder & $aFiles[$aFiles_Index[$i]] & @CRLF)
            _ArrayAdd($aErr, $aFiles[$aFiles_Index[$i]])    ;Add the filename to the Error Array

        Else

            ConsoleWrite("Move...:" & $S_Folder & $aFiles[$aFiles_Index[$i]] & @CRLF & "To.....:" & $S_Folder & $S_Prefix & StringFormat("%03d", $i_Start) & Get_Extension($aFiles[$aFiles_Index[$i]]) & @CRLF & "+Done...." & @CRLF)

            $i_Start += 1

        EndIf
        ;String Format is used to make the numeral part of the name minimum of 3 digits.
        ;Get_Extension func returns the extension of the file with a period prefixed with it.

    Next

    _ArrayDelete($aErr, 0)

    ;Set Extended to True if even one file is renamed.
    Return SetExtended($i_Start > 1, $aErr) ;Return "" if all sucess otherwise return the array of the errors
EndFunc   ;==>Rename_Files

#cs
    Wildcards
    *  - Zero or more character
    +  - One or more character
    ?  - No or one character
#ce
; #FUNCTION# ====================================================================================================================
; Name ..........: _ArrayFindAllEx
; Description ...: Similar to _ArrayFindAll with Include, Exclude masks
; Syntax ........: _ArrayFindAllEx(Const Byref $aArray, $sIncludeMask[, $fIncSenstive = True[, $sExcludeMask = ''[,
;                  $fExcSenstive = True]]])
; Parameters ....: $aArray              - [in/out and const] The Array to search for the values.
;                  $sIncludeMask        - A string value.
;                  $fIncSenstive        - [optional] A boolean value. Default is True.
;                  $sExcludeMask        - [optional] A string value. Default is ''.
;                  $fExcSenstive        - [optional] A boolean value. Default is True.
; Return values .: Sucess - Returns the array of the Index of the found values
;                         - @extended is set to the number of items found
;                  Failure  -  Returns '' and sets @error to 1
; Author ........: Phoenix XL
; Modified ......:
; Remarks .......:
; Related .......: _ArrayFindAll
; Link ..........: http://www.autoitscript.com/forum/topic/147216-solved-is-there-any-arrayfindall-replacement-that-support-wildcard/#entry1043378
; Example .......: Yes
; ===============================================================================================================================
Func _ArrayFindAllEx(ByRef Const $aArray, $sIncludeMask, $fIncSenstive = True, $sExcludeMask = '', $fExcSenstive = True)
    ;Set Sensitivity Values
    If $fIncSenstive Then
        $fIncSenstive = ''
    Else
        $fIncSenstive = '(?i)'
    EndIf

    ;Make the Include Mask Pattern
    $sIncludeMask = StringRegExpReplace($sIncludeMask, '(?s)([^\w|])', '[\1]')
    $sIncludeMask = StringRegExpReplace($sIncludeMask, '(?s)(\[\?\])', '.?')
    $sIncludeMask = StringRegExpReplace($sIncludeMask, '(?s)(\[\+\])', '.+')
    $sIncludeMask = $fIncSenstive & '(?s)\A(' & StringRegExpReplace($sIncludeMask, '(?s)(\[\*\])', '.*') & ')\z'

    Local $aRet[1]

    ;Debug Out Include Mask Patterns
;~  ConsoleWrite($sIncludeMask & @CR)

    ;Get the to-include Strings
    For $i = 0 To UBound($aArray) - 1
        If StringRegExp($aArray[$i], $sIncludeMask) Then _ArrayAdd($aRet, $i)
    Next
    _ArrayDelete($aRet, 0)
    If Not IsArray($aRet) Then Return 0
    If Not $sExcludeMask Then Return SetExtended(UBound($aRet), $aRet)

    ;Set Sensitivity Values
    If $fExcSenstive Then
        $fExcSenstive = ''
    Else
        $fExcSenstive = '(?i)'
    EndIf

    ;Make the Exclude Mask Pattern
    $sExcludeMask = StringRegExpReplace($sExcludeMask, '(?s)([^\w|])', '[\1]')
    $sExcludeMask = StringRegExpReplace($sExcludeMask, '(?s)(\[\?\])', '.?')
    $sExcludeMask = StringRegExpReplace($sExcludeMask, '(?s)(\[\+\])', '.+')
    $sExcludeMask = $fExcSenstive & '(?s)\A(' & StringRegExpReplace($sExcludeMask, '(?s)(\[\*\])', '.*') & ')\z'

    ;Debug Out Exclude Mask Patterns
;~  ConsoleWrite($sExcludeMask & @CR)

    ;Delete the to-exclude strings
    For $i = UBound($aRet) - 1 To 0 Step -1
        If StringRegExp($aArray[$aRet[$i]], $sExcludeMask) Then _ArrayDelete($aRet, $i)
    Next
    ;Done
    Return SetError(Not IsArray($aRet), UBound($aRet), $aRet)

EndFunc   ;==>_ArrayFindAllEx


Func Get_Extension($S_File)
    ;Returns the extension of the file with the period.
    Return StringRegExpReplace($S_File, ".*(\.)", "\1")
EndFunc   ;==>Get_Extension

Regards :)

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

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  

×