Jump to content

Renaming of files


Recommended Posts

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.txt

But when there is 2 files. Like test.txt and  text050102.txt

It does not rename the test.txt file

where 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)
next

Ini 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

 

Link to comment
Share on other sites

Now i have it working. Even with a backup 2 a zip file option.

 

#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

 

Link to comment
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
 Share

×
×
  • Create New...