Jump to content
Sign in to follow this  
MyEarth

Rename based on file name

Recommended Posts

MyEarth

Hello

I'd like to do a task. I have ( or not have, depends on user select ) a number file list like:

File_1.txt
File_2.txt
File_3.txt
File_4.txt
File_5.txt

If a user delete a file, like:

File_1.txt
File_2.txt
File_4.txt
File_5.txt

I want reneame the other file list, in this way

File_1.txt
File_2.txt
File_3.txt  (Previusly was File_4.txt)
File_4.txt  (Previusly was File_5.txt)

I don't know how to check if file exist or not based on the number or file, then rename the rest of the list. Thanks

Share this post


Link to post
Share on other sites
jdelaney

_FileListToArray

You can have a loop, with a counter itterating in it, where you then

FileMove($actualname, $newname)


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
MyEarth

I know i have to do an array and use FileMove ( i have read the guide about it before post ) but i want to reneame the files only if necessary ( 123567 4 is not here then reneame 567 to 456), not everytime.

So i need to check if a number not exist or something like it, dunno

Edited by MyEarth

Share this post


Link to post
Share on other sites
jdelaney

#include <Array.au3>
$dir = @DesktopDir & "\test\"
$aFiles = _FileListToArray($dir)
Local $aSortedNumerically[UBound($aFiles)-1][2]
For $i = 1 To UBound($aFiles) - 1
 $aSplit = StringSplit($aFiles[$i], "_")
 $aSortedNumerically[$i-1][0] = $aSplit[1] & "_"
 $aSortedNumerically[$i-1][1] = Int($aSplit[2])
Next
_ArraySort($aSortedNumerically, 0,0,0,1)
;~ _ArrayDisplay($aSortedNumerically)
$iCounter = 1
For $i = 0 To UBound($aSortedNumerically) - 1
 ConsoleWrite($iCounter & " " & $aSortedNumerically[$i][1] & @CRLF)
 If $iCounter<>$aSortedNumerically[$i][1] Then
  FileMove($dir & $aSortedNumerically[$i][0] & $aSortedNumerically[$i][1] & ".txt", $dir & $aSortedNumerically[$i][0] & $iCounter & ".txt")
 EndIf
 $iCounter += 1
Next


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

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  

×