Saurabh2k26,
You could also do it this way.
#include <File.au3>
Opt("MustDeclareVars", 1)
Global $sPathToFiles = "D:\test\"
Global $newFileName = 1000
Global $aFilesToRename = ""
;Get file names to rename
$aFilesToRename = _FileListToArray($sPathToFiles, "*.jpg", 1)
;Loop through file names to Rename
For $i = 1 To $aFilesToRename[0]
;increment new file name by 1 with each pass of loop. 1001.jpg, 1002.jpg etc
$newFileName += 1
;Rename files
FileMove($sPathToFiles & $aFilesToRename[$i], $sPathToFiles & String($newFileName) & ".jpg", 0)
Next
-
This version is a little bit more involved but not by much - here is the script with some error checking and user prompting.
#include <GUIConstantsEx.au3>
#include <File.au3>
#include <Array.au3>
Opt("MustDeclareVars", 1)
Global $sPathToFiles = "D:\test\"
Global $newFileName = 1000
Global $aFilesToRename = ""
_Main()
Func _Main()
_GetFileNamesToRename()
_ArrayDisplay($aFilesToRename) ;Show me found files
_RenameTheFiles()
EndFunc ;==> _Main()
Func _GetFileNamesToRename()
$aFilesToRename = _FileListToArray($sPathToFiles, "*.jpg", 1)
;Check if _FileListToArray() returned an error
If $aFilesToRename = 0 Then
Select
Case @error = 1
Msgbox(48, "Error", "Path not found or invalid." & @CRLF & "Terminating script")
Exit
Case @error = 2
Msgbox(48, "Error", "Invalid file filter. [$sFilter]." & @CRLF & "Terminating script")
Exit
Case @error = 3
Msgbox(48, "Error", "Invalid Flag. [$iFlag]" & @CRLF & "Terminating script")
Exit
Case @error = 4
Msgbox(48, "Error", "No File(s) Found" & @CRLF & "Terminating script")
Exit
EndSelect
EndIf
EndFunc ;==> _GetFileNamesToRename()
Func _RenameTheFiles()
Local $iMsgboxReturn
;Alert user files are about to be renamed / Ask user for confirmation via buttons
$iMsgboxReturn = Msgbox(64+1, "Confirm Rename", $aFilesToRename[0] & chr(32) _
& "File(s) in this directory:" & @CRLF & $sPathToFiles & chr(32) _
& @CRLF & "will be renamed.")
;Check confirmation (1 = ok btn, 2 = cancel btn)
If $iMsgboxReturn = 2 Then
Msgbox(16, "File Rename cancelled", "File Rename canceled." & @CRLF & "Click OK to terminate file rename.")
Exit
EndIf
;Loop through file names to Rename
For $i = 1 To $aFilesToRename[0]
;increment new file name by 1 with each pass of loop
$newFileName += 1
;Rename files
FileMove($sPathToFiles & $aFilesToRename[$i], $sPathToFiles & String($newFileName) & ".jpg", 0)
;Alert user files have been renamed
If $i = $aFilesToRename[0] Then
MsgBox(64, "Files Renamed", $i & chr(32) & "Files have been renamed.")
Exit
EndIf
Next
EndFunc ;==> _RenameTheFiles()