A fix for folder

Advanced Renamer forum
#1 : 14/02-25 08:01
Delta Foxtrot
Posts: 421
Hi all,

(The title of this post keeps getting cut off (at least in my browser) so here it is:

A fix for folder "level collision" problem when using "Add Subfolders"

NOTE: If you don't have problems with folder renaming on multiple directory levels at the same time this will not help you. In that case it's definitely [TL;DR]

I know this is not a problem in most instances, but I encounter it reasonably often, and the results are annoying at best. Someone recently posted about the problem (I couldn't find the thread on cursory search) so I know at least one other person has encountered this situation.

The problem: You drag in a ton of folders using "Add subfolders" (and possibly additionally "Add folders" ). You then have folders that are children of other folders in the list, and the parent folders are children of still other folders in the list. You check the "New filename" column and everything looks groovy, but when you execute the batch you get a bleep -ton of errors because ARen changes names of parent folders before it gets to child folders, creating a situation where there is no path to the child folders as originally loaded. And ARen doesn't take into account the changed parent folders; hence the errors; or worse still, if you have a collision rule applies, like add number or add pattern, ARen creates a new path (well, actually the old path that no longer exists) in a futile effort to find a directory that no londer exists. So it creates THAT directory and adds the collision rule addition to it. So now you've got a slew of folders that mimic what you'd had before the batch execution, containing nothing.

I've had a lot of experience dealing with the aftermath of that stuff, and I've been trying to figure a way to circumvent the problem given the tools available within ARen. I even tried PowerShell (which I hate) and Python (which is ok but really clunky for this application). Finally, I've created a solution and a workflow to deal with the problem within ARen. Most users will never encounter the problem, but if you deal with deeply-nested folders for rename (or even shallowly-nested folders where you need to also rename a lot of the containing folders) you might benefit from this sequence.
NOTE: I've tested this out a bit, and it's worked fine, but be ready to undo batches if it screws up.

1. Set Collision avoidance to "Fail". (if something goes wrong you don't want bogus folders created)
2. In "Settings / Renaming / Add files when batch ends:" to "All items". (this way you can execute
multiple directory-level changes without having to figure out what to load; just start with the
deepest level folders and work your way up the directory tree.
2. Load your method list and add this script as the last item:

(EDIT 3:)
SCRIPT REMOVED. SEE NEXT POST FOR CURRENT SCRIPTS
(END EDIT)

The script looks at each entry in the File list (actually folder list) and computes whether it is on the
depth level within the directory tree denoted by the third line (depth = n). Example:
C:\Museums\USA\New York\New York City\MOMA\Paint\Artist\<Name>\*.*
(The deepest level here is 10, just count backslashes and add 1). Lets say you want to add ID numbers to several of the directory levels, but also want to make changes in the deepest level, and you've created a method batch that does it all in one batch, so you'll eventually end up with
C:\Museums\04 - USA\33 - New York City\22 - MOMA\Paint\Artist\<Artist ID> <Name>\*.*
(This is something I've actually had to do, although MOMA has never been one of my clients :)

I start with the deepest level to be changed (in this case level 10, as the script denotes). Execute the batch with the depth set to that level; the files will be reloaded after execution and you must then change the "depth =" line to the next applicable level (in my case 9, the <Artist name> -> <ID> <Name> level.
Execute again; then repeat for each level that needs to be changed.

If you start with the deepest level you need to change, then work your way up the directory tree, you don't have to reload files; ARen will do that for you.

EDIT: Two notes: 1. Don't forget to click "Apply script after changing the depth level. 2. You may get error messages if you have folders nested even deeper than your deepest level that you change; they don't mean anything except ARen can't find those directories (that you aren't changing anyway). END EDIT

Complicated? Slightly. I'd rather ARen had an option to only operate on a specific directory depth level, but for now it doesn't. This is a work-around for "in the meantime", and it works for me. You'll have to decide if it works for you, but in hopes it might I'm posting it here.

EDIT 2: BTW, this doesn't address what I call the "period bug" in script methods. If you have folders with periods in the names, using a script method causes ARen to insert everything after the first period *again* at the folder name end. In the past I had a lot of periods in folder names, and now I have to pre-process files in a separate batch to replace all periods with another character, or characters, before I run any batch that contains a script method (just adding a replace method before a script method doesn't work). Hopefully Kim will address this soon, but in the meantime that's the workflow I've found to work for me.

On another topic, I'm going to add a debug version of this script (as soon as I get it the way I want). With the debug version you'll be able to check the JS Console to see what's going on. It's useful for me when I have lots of directory levels and its not intuitively obvious how to proceed. Check back if you're interested.
END EDIT 2

Happy renaming!
DF

edited: 15/02-25 21:18
#2 : 15/02-25 21:22
Delta Foxtrot
Posts: 421
OK, so I messed up that first script. Here's the real thing:

// ---------------------------------------------------------------
// NEXT LINE GOES IN PRE-BATCH SCRIPT - REMOVE DOUBLE-SLASH :
// deepest = 0 ;
// END PRE-BATCH
//---------------------------------
// ----------------------- ! NODEBUG ! ----------------------------
// ENTER DEPTH HERE:
depth = 10 ;

folderName = item.newBaseName ;
path = item.path ;
level = path.split( "\\" ).length ;
if ( level > deepest ) { deepest = level }
if ( app.currentIndex == ( app.itemCount - 1 ) ) { app.log( "****** DEEPEST LEVEL = " + deepest ) ; }
if ( level != depth ) {
return item.name ;
} else {
return folderName ;
}
// ---------------------------------------------------------------

You'll need to move the second line ("deepest = 0 ;") to the Pre-Batch Script, and remove the double slashes at the start; this computes the deepest level in your files list. That's the only thing that will appear in the "Tools / JS Console" window. Otherwise just follow the directions in the first post. Again, the most efficient way to run this is to start at the deepest level and work up; that way you don't change any parent/grandparent folder names and have to reload the entire files list.

And here's a debug version, so you can see in the JS Console what's going on under the hood. You can also turn off debug so it acts like the no-debug script. Unless your computer is really slow or you are working with folder lists in the tens of thousands the no-debug mode will probably work just fine for you.

// ---------------------------------------------------------------
// NEXT LINE GOES IN PRE-BATCH SCRIPT - REMOVE DOUBLE-SLASH :
// deepest = 0 ;
// END PRE-BATCH
//---------------------------------

// ! DEBUG VERSION !
// SET TO 1 FOR DEBUG INFO TO JS CONSOLE; SET TO 0 FOR NO-DEBUG //
debug = 1 ;

// ENTER DEPTH HERE:
depth = 5 ;

folderName = item.newBaseName ;
oldFolderName = item.name ;
path = item.path ;
level = path.split( "\\" ).length ;

if ( level > deepest ) { deepest = level }
if ( app.currentIndex == ( app.itemCount - 1 ) ) { app.log( "****** DEEPEST LEVEL = " + deepest ) ; }

if ( level != depth ) {
if ( debug == 1 ) {
app.log("#" + ( app.currentIndex + 1 ) + " REVERT Level " + level + " " + path + folderName ) ;
app.log( "————————————————————————————————————" ) ;
}
return oldFolderName ;
} else {
if ( folderName != oldFolderName ) {
if ( debug == 1 ) {
app.log( " " ) ;
app.log( "#" + ( app.currentIndex + 1 ) + " TARGET DEPTH = " + depth ) ;
app.log( " " ) ;
app.log( " " ) ;
app.log( "Old: "+'"' + path+oldFolderName + '"');
app.log( "NEW: "+'"' + path+folderName + '"');
app.log( " " ) ;
app.log( " " ) ;
app.log( "******** COMMIT CHANGES - Level " + level + " ********" ) ;
app.log( " " ) ;
app.log( "————————————————————————————————————" ) ;
}
return folderName ;
} else {
if ( debug == 1 ) {
app.log( "#" + ( app.currentIndex + 1 ) + " UNCHANGED. Level " + level + " " + path + folderName ) ;
app.log( "————————————————————————————————————" ) ;
}
return oldFolderName ;
}
}
// ---------------------------------------------------------------

Again, don't forget to put "deepest = 0;" in the Pre-Batch Script.

EDIT: It's a good idea to get in the habit of deactivating this after using it. Trust me, it's real drag working on a change in your batch and it's just not working, then after 15 minutes of fiddling you realize this script is set to a different level than you are working on! :)
END EDIT

Best,
DF

edited: 15/02-25 23:38