Share this post

Link to post

Share on other sites

orbs 172

orbs 172

use _FileListToArray() to read the files into array. by default it is sorted, but that may depend on the OS (Win2k for example does not sort), so use _ArraySort() on the array, then loop the array to rename each file in order.

but let's start simple, ok? start by reading the help for _FileListToArray() (that reading would be less than a minute, really). then adapt the example to the target folder and file mask, and filter for files only.

i'm sure that when you get the concept of reading the help file and using the examples provided, you will need no further help to apply _ArraySort() and For loop on the array, and FileMove() to rename each file.

if by then you need any help, post whatever code you managed to write, and we will help you to get it running.

Share this post

Link to post

Share on other sites

saurabh2k26 0

saurabh2k26 0

I saw FileMove() function and i could not understand how to rename a file using it.

Test Data:

2013-12-12.jpg

2013-12-15.jpg

2013-12-17.jpg

.......

Result Data:

1001.jpg

1002.jpg

1003.jpg

.......

So, i need to use a variable VAR with initial value 1000. Find 1st file using _FileListArray and rename it as $VAR and then append $VAR by 1 in each loop and then remane 2nd file by new $VAR and so on.

Share this post

Link to post

Share on other sites

AndrewG 3

AndrewG 3

#include <File.au3>Opt("MustDeclareVars",1)Global$sPathToFiles="D:\test\"Global$newFileName=1000Global$aFilesToRename="";Get file names to rename$aFilesToRename=_FileListToArray($sPathToFiles,"*.jpg",1);Loop through file names to RenameFor$i=1To$aFilesToRename[0];increment new file name by 1 with each pass of loop. 1001.jpg, 1002.jpg etc$newFileName+=1;Rename filesFileMove($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=1000Global$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 errorIf$aFilesToRename=0ThenSelectCase@error=1Msgbox(48,"Error","Path not found or invalid."&@CRLF&"Terminating script")ExitCase@error=2Msgbox(48,"Error","Invalid file filter. [$sFilter]."&@CRLF&"Terminating script")ExitCase@error=3Msgbox(48,"Error","Invalid Flag. [$iFlag]"&@CRLF&"Terminating script")ExitCase@error=4Msgbox(48,"Error","No File(s) Found"&@CRLF&"Terminating script")ExitEndSelectEndIfEndFunc;==> _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=2ThenMsgbox(16,"File Rename cancelled","File Rename canceled."&@CRLF&"Click OK to terminate file rename.")ExitEndIf;Loop through file names to RenameFor$i=1To$aFilesToRename[0];increment new file name by 1 with each pass of loop$newFileName+=1;Rename filesFileMove($sPathToFiles&$aFilesToRename[$i],$sPathToFiles&String($newFileName)&".jpg",0);Alert user files have been renamedIf$i=$aFilesToRename[0]ThenMsgBox(64,"Files Renamed",$i&chr(32)&"Files have been renamed.")ExitEndIfNextEndFunc;==> _RenameTheFiles()