Delete older file versions

Advanced Renamer forum
#1 : 26/11-24 12:14
donpablo
Posts: 3
Which methods can I use to rename the following files AND delete the older versions?

old:
IMG_Cat (2024-04-13).jpg
IMG_Cat (2024-05-10).jpg
IMG_Cat (2024-05-29).jpg
IMG_Cat (2024-06-11).jpg
IMG_Cat (2024-07-18).jpg
IMG_Cat_v02 (2024-05-10).jpg
IMG_Cat_v02 (2024-05-23).jpg
IMG_Cat_v02 (2024-08-30).jpg
IMG_Cat_v03 (2024-06-21).jpg
IMG_Cat_v03 (2024-10-17).jpg

new:
IMG_Cat (2024-07-18).jpg
IMG_Cat_v02 (2024-08-30).jpg
IMG_Cat_v03 (2024-10-17).jpg

Many thanks for your help!

edited: 26/11-24 15:09
#2 : 27/11-24 02:28
Delta Foxtrot
Posts: 364
Reply to #1:

Hi Don, welcome,

In Advanced Renamer there is technically no way to *delete* a file (send to the recycle bin or do a "hard" delete either). In those situations I have a folder (I call it "#recycle" and it's in the root directory of my work disk). When I want to delete something I just have ARen move it to that folder. The problem is that you can't move *some* files and leave others using the MOVE mode. I have some scripts to do that for me, where depending on whatever conditions I set the "item.newPath" internal variable gets set to that #recycle folder.

In your case it will take a script to do something akin to what you want, since ARen works on one item at a time and can't do filename comparisons across multiple filenames without scripting. I've been working on some code to do it, but it's not ready yet. In the meantime, can you clarify what your filenames look like? Are they ALWAYS named "IMG_Cat"+ a date in parenthesis, with some having a version number denoted by "_v" + two digits, between the first part and the date part? Or do you actually have various names followed by the same version + date pattern? Is the version number ALWAYS two digits?

Oh, tell me if you are using windows and the latest version of ARen; I don't know what works on a Mac and this script definitely won't work on a previous version of Aren.

Best,
DF

edited: 27/11-24 02:30
#3 : 27/11-24 14:03
donpablo
Posts: 3
Reply to #2:

Hi Delta Foxtrot,

Thank you for your reply.

Too bad AR can't delete files. But it is a good tip to move the files to be deleted to a #recycle folder.

- The file names have different names with different lengths:
Img_Cat.jpg, Picture_Dog.jpg, Animals.jpg, etc.

- The version numbers always have two numbers (_vXX)
But not every file has a version number... (see original post)

- This is always followed by a space and the backup date in brackets according to the following format:
(2023_11_24 14_42_56 UTC)

filenameXYZ_vXX (yyyy_mm_dd hh_mm_ss UTC)
or
filenameXYZ (yyyy_mm_dd hh_mm_ss UTC)

I am using the latest version of AR on Windows 10.

I hope this answers all your questions.
A script for this would be awesome of course :D

Regards
Don
#4 : 28/11-24 07:03
Delta Foxtrot
Posts: 364
Reply to #3:

Hey Don,

Well, that was... interesting. :)

I ***think*** this works for pretty much any file in the format you outlined, but I can't test it in all of the edge possibilities. You'll have to load it and carefully review the preview column ("New Filename", as well as the "New Path" column to make sure the files are going where you want. And remember, if you run a batch and get suboptimal results, just undo that batch and you'll be back to where you started. The only thing you need to make sure of is that the original filenames are sorted so that the names and versions are together. They can be ascending or descending, but not out of order. That would be beyond my pay grade. :)

Ok, here you go. I don't know if you've used scripts in ARen before, but there are two parts to this one, the PRE-BATCH script, which only runs once but in this case does most of the work; and the MAIN script, which runs one time for each file.

Here's what you'll need to do:
1. On your work drive, create a "#recycle" folder in the root directory. You can name it whatever you want, but then you'll have to change it in the script (it's the fourth line in the PRE-BATCH section, just don't remove or add anything but the folder name).

2. On a clean instance of ARen (or at the bottom of a method list you use) open a script method. Find the "Pre-batch script…" button and open that window. Cut and paste the first code block below into that window, then press the "Close and apply…" button at the bottom right.

//------------- PRE-BATCH:
// VARIABLES THAT MIGHT NEED TO BE ADJUSTED:
// change the following string if you use a different delete-folder:
let delDir = "#recycle" ;
// change the following if your delete-folder isn't in root directory:
delPath = app.parseTags( "<FolderName:-1>:\\" ) + delDir ;
// VARIABLES THAT SHOULDN'T NEED ADJUSTMENT
// Initialize array:
const delArr = [] ; // holds index of records to 'delete' (change path)
// Previous filename information:
let prevName = null ;
let prevVersion = null ;
let prevDate = null ;
// This version information:
let thisFilename = "" ;
let thisName = "" ;
let thisVersion = null ;
let thisDate = null ;
// counters:
let j = 0 ; // Populate array loop (index) counter
let k = 0 ; // delete array index counter
// filename pattern
const filePattern = /^([^~]+?)~(\d{2}) \((\d{8}).*$/ ;
// VARS INITIALIZED; LOOP TO POPULATE ARRAYS:
while ( j < app.itemCount ) {
// Populate vars:
thisFilename = fNameTRX( app.getItem(j).name ) ; // TRXd filename at index j
thisName = fReplace( thisFilename, filePattern, "$1" ) ; //root filename (minus version & date)
thisVersion = fReplace( thisFilename, filePattern, "$2" ) ; // Version (2-digit string)
thisDate = fReplace( thisFilename, filePattern, "$3" ) ; // Date (8-digit string)
// Determine if first filename state:
if ( j == 0 ) { // first record
prevName = thisName ;
prevVersion = thisVersion ;
prevDate = thisDate ;
j++
continue ;
} else {
}
// Determine which state, proceed accordingly:
// follow-up state: Name & version match previous:
if ( ( prevName == thisName ) && ( prevVersion == thisVersion ) ) {
if ( thisDate * 1 > prevDate * 1 ) { // add prev item to delArr
delArr[k] = ( j - 1 ) ;
k++ ;
} else { // add this item to delArr:
delArr[k] = j ;
}
prevName = thisName ;
prevVersion = thisVersion ;
prevDate = thisDate ;
j++ ;
k++ ;
continue ;
}
// New Version state: start comparison over:
if ( ( prevVersion !== thisVersion ) || (prevName !== thisName ) ) {
prevName = thisName ;
prevVersion = thisVersion ;
prevDate = thisDate ;
j++
continue ; // start loop again with current information
}
}
//--------- USER-DEFINED FUNCTIONS
function fReplace( origFilename, rgxPattern, replWithStr ) {
// ( string, regexp, string )
const retString = origFilename.replace( rgxPattern, replWithStr ) ;
return retString ;
}
function fNameTRX( origFilename ) {
let pattern = /_v(\d\d)/ ;
let returnStr = "~$1" ;
let newName1 = "" ;
let newName2 = "" ;
newName1 = fReplace( origFilename, pattern, returnStr ) ;
newName2 = newName1 ;
pattern = /([^\d]) \(/ ;
returnStr = "$1~01 ("
newName1 = fReplace( newName1, pattern, returnStr ) ;
newName2 = newName1 ;
pattern = /\((\d{4})_(\d{2})_(\d{2})/ ;
returnStr = "($1$2$3" ;
newName1 = fReplace( newName1, pattern, returnStr ) ;
return newName1 ;
}
//------------------------------ END PRE-BATCH SCRIPT

3. Copy the following code and paste it into the blank main script window you'll see at this point.
Oh, there are two versions. To simplify the task I added "_v01" to all the files without a version. The first script version leaves the files that way (because I like it, but you may not). The second version reverts filenames back to "no version" names if that's what you want. Here are the main scripts:

//------------- MAIN SCRIPT 1 (version number 01 included):
const testFilename = fNameTRX( item.newBasename ) ;
for ( j = 0 ; j <= app.itemCount ; j++ ) {
if ( delArr[j] == app.currentIndex ) {
item.newPath = delPath ;
}
}
let newName1 = fReplace( testFilename, /~/, "_v" ) ;
let pattern = /\((\d{4})(\d{2})(\d{2})/ ;
let newName2 = fReplace( newName1, pattern, "($1_$2_$3" ) ;
return newName2 ;
//--------------------- END SCRIPT

And here is the "no version" version:

//------------- MAIN SCRIPT - version 01 removed:
const testFilename = fNameTRX( item.newBasename ) ;
for ( j = 0 ; j <= app.itemCount ; j++ ) {
if ( delArr[j] == app.currentIndex ) {
item.newPath = delPath ;
}
}
let newName1 = fReplace( testFilename, /~/, "_v" ) ;
let pattern = /\((\d{4})(\d{2})(\d{2})/ ;
let newName2 = fReplace( newName1, pattern, "($1_$2_$3" ) ;
newName1 = fReplace( newName2, /_v01/, "" ) ;
newName2 = newName1 ;
return newName2 ;
//----------------------- END SCRIPT

Press the "Apply script" button (top right).

4. Load your files, preview, and if something's not right hit me back.

(I'll put a screenshot of my results in the next post)

Best,
DF

edited: 28/11-24 07:14
#5 : 28/11-24 07:13
Delta Foxtrot
Posts: 364
Reply to #4:
Hey Don,

Screenshot: https://drive.google.com/file/d/1D13GCoUgX_OREX5 JDiuo_jRPnBVRc0h5/view?usp=sharing

Happy motoring! :)

Best,
DF
#6 : 29/11-24 10:48
donpablo
Posts: 3
Reply to #4:
Thank you very much for your efforts.
I will try it out at the next opportunity.