climmax Posted August 28, 2015 Posted August 28, 2015 Hi all,As a newby i need some help.I am trying to add time stamps to file names.But it has to do it only once. So i made a check on lenght of filename.If filename extention and Lenght is correct it renames the file with a time stamp.I use a ini file for settings. and extentions what needs to be renamed.The script works but only once.it renames all the files that are in the ini . like test.txt to text050102.txtBut when there is 2 files. Like test.txt and text050102.txtIt does not rename the test.txt filewhere am i doing this wrong? #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> $path = IniRead("RenameFiles.ini","General","InputFolder","Error") $TimeOption = IniRead("RenameFiles.ini","General","TimeOption","Error") Local $aArray = iniReadSection("RenameFiles.ini","EXT") For $i = 1 To $aArray[0][0] $search01 = FileFindFirstFile($path&"*"&$aArray[$i][0]) If $aArray="" Then MsgBox(0,"Error","Check RenameFiles.ini","","") Exit EndIf While 1 $File = FileFindNextFile($search01) $StringLenght= StringLen ($File) if $StringLenght= $aArray[$i][1] Then $FileSplit= StringSplit($File,".") $Time=FileGetTime ( $File ,$TimeOption ,0 ) If @error Then ExitLoop FileMove($path & $File, $path & $FileSplit[1] & $Time[3] & $Time[4] & $Time[5] & $aArray[$i][0], 0) Else ExitLoop EndIf WEnd FileClose($search01) nextIni file looks like:[General] ;Enter folder where the files are located: ;Example C:\test\ InputFolder=C:\FileRename\ ;Enter the option you want to use, for timestamp ;0 = Last modified (default) ;1 = Created ;2 = Last accessed TimeOption=0 ;Enter Files extensions that needs a timestamp. ;FileExtention=FileNameLenght (characters) ;Example .TRX.MAU (Dont forget the leading point) ;FileNameLenght = how many characters the filename contains. Including "." ;If filename is longer or shorter it wil not rename the file. ;Example TR150730.TRX.MAU = 16 characters. [EXT] .txt=8 .log=8
jguinch Posted August 28, 2015 Posted August 28, 2015 Well, you cannot have two file with the same name.You should check if a file with the new name exists and then change the new name to text050102-2.txt (for example) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
DarkwarlorD Posted August 28, 2015 Posted August 28, 2015 @climmaxAs an option, instead looking for the lenght of the name, try to add some symbol before the timestamp (can be configured on the ini), then just check if the symbol exist (with StringInStr or RegEx) to don't rename again.About the two files don't be renamed, try to look if they have diferent timestamp, as @jguinch said.
climmax Posted August 31, 2015 Author Posted August 31, 2015 Thanks for the reply'sMaybe im doing this all wrong. I will start over and maybe use _filelisttoarray as a array.
climmax Posted September 3, 2015 Author Posted September 3, 2015 Now i have it working. Even with a backup 2 a zip file option. expandcollapse popup#include <MsgBoxConstants.au3> #include <File.au3> #include <Array.au3> #include <WinAPIFiles.au3> #include "Zip.au3" $path = IniRead("BackupRename.ini","General","InputFolder","Error");Read input folder for renaming from BackupRename.ini. $TimeOption = IniRead("BackupRename.ini","General","TimeOption","Error");Read time option path from BackupRename.ini. $aArray = iniReadSection("BackupRename.ini","EXT");Read Extentions from BackupRename.ini. $BackupFolder=IniRead("BackupRename.ini","General","BackupFolder","Error");Read Backupfolder path from BackupRename.ini. If not $BackupFolder="" Then ;If Backupfolder is not empty in BackupRename.ini then create a zip. $Zip = _Zip_Create($BackupFolder&"Backup["&@YEAR&@MON&@MDAY&"]["&@HOUR&@MIN&@SEC&"].zip") EndIf Rename() Func Rename() For $i = 1 To $aArray[0][0] Local $hSearch = FileFindFirstFile($path&"*"&$aArray[$i][0]); Assign a Local variable the search handle of all files in the current directory. Local $sFileName = "", $iResult = 0; Assign a Local variable the empty string which will contain the files names found. While 1 $sFileName = FileFindNextFile($hSearch) ; If there is no more file matching the search. If @error Then ExitLoop $Time=FileGetTime ( $path&$sFileName ,$TimeOption ,0 );Find the Timestamp from file if @error = 1 then ;msgbox(0,'','Bad return from filegettime'&$sFileName) ExitLoop endif $StringLenght= StringLen ($sFileName);find string lenght from filename if $StringLenght= $aArray[$i][1] Then $FileSplit= StringSplit($sFileName,".");Split file name where there is a "." if @error = 1 then ;msgbox(0,'','Bad return from Stringsplit') ExitLoop EndIf If not $BackupFolder="" Then; if BackupFolder is not empty add file to sip. ;MsgBox("","","Zip="&$Zip&"file="&$sFileName) _Zip_AddFile($Zip,$path&$sFileName) EndIf FileMove($path & $sFileName, $path & $FileSplit[1] & $Time[3] & $Time[4] & $Time[5] & $aArray[$i][0], 1) EndIf WEnd FileClose($hSearch); Close the search handle. next;Go to next file EndFunc ;==>Rename if _Zip_Count($Zip) ="0" Then ;If no files are added to zip file. Delete the zip FileDelete ($Zip) EndIf[General] ;Enter folder where the files are located: ;Example C:\test\ InputFolder=C:\FileRename\ BackupFolder=C:\FileRename\Backup\ ;Enter the option you want to use, for timestamp ;0 = Last modified (default) ;1 = Created ;2 = Last accessed TimeOption=0 ;Enter Files extensions that needs a timestamp. ;FileExtention=FileNameLenght (characters) ;Example .TRX.MAU (Dont forget the leading point) ;FileNameLenght = how many characters the filename contains. Including "." ;If filename is longer or shorter it wil not rename the file. ;Example TR150730.TRX.MAU = 16 characters. [EXT] .txt=8 .log=8
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now