Find Sort Order and Count Character Position

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
RNIB
Macro Veteran
Posts: 193
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Find Sort Order and Count Character Position

Post by RNIB » Thu Jul 18, 2024 9:32 am

Bit of a complicated one to explain.

I have around 60,000 folders which all contain a mix of files including MP3's. Within each folder the MP3's will be named in the same way but there will be something in the file name, usually a number, that will denote the order by which the files can be sorted so that they play in the correct order. However, whilst the MP3's in each folder will be named the same way, this will vary from folder to folder.

We've found that some operating systems and/or firmware of a few audio players can play the MP3 files in the wrong order because either it cannot read the full length of the file name or it only applies sorting to the first x characters of the file name.

Ways the MP3's could be named include:

Example 1:
01_Chapt.mp3
02_Chapt.mp3

Example 2:
001Chapt.mp3
002Chapt.mp3

Example 3:
123456_0_001a_title.mp3
123456_0_002a_title.mp3

Example 4:
dtb-ae123001.mp3
dtb-ae123002.mp3

In each, the number increments by one for each MP3 file.

What I want to do is to identify the folders that contain MP3's where the character that defines the sort order occurs higher than position 8 in the file name. So, in the first two examples the sort order is defined by the first 2 and 3 characters and so it doesn't need to do anything. In the 3rd and 4th though it's not until the 12th character.

I know how to capture all the files names but I can't work out how to work out the sort order and count the position of the character that does this. I had wondered about looking for a unique number in each position but that only works for 01-09. As soon as you go 11,12 you no longer have a unique number in each position. As I don't know how many characters make up the number or where they will occur within the file name, I've no idea how to tell the macro what to look for. Can this be done with Macro Scheduler?

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Find Sort Order and Count Character Position

Post by hagchr » Thu Jul 18, 2024 4:26 pm

Hi, I think you can use regex to find the sort order, or to check if sort order is higher than position 8. The following lines will extract the sort order for all examples (by taking the right-most digits in each case), and separately, also extracting the sort order, only for examples with numbers higher than position 8. If regex is greek to you then let me know and I can elaborate... Hope it helps.

Code: Select all

LabelToVar>strData,strData

RegEx>(?m-s)\d+(?=[^\d]*\.mp3),strData,0,m,nm,0
RegEx>(?m-s).{8}\K\d+(?=[^\d]*?\.mp3),strData,0,m2,nm2,0

Let>res=
Let>k=0

While>k<nm
  Add>k,1
  Let>tmp=m_%k%
  Let>res=%res%%tmp%%CRLF%
EndWhile


Let>res2=
Let>k=0

While>k<nm2
  Add>k,1
  Let>tmp=m2_%k%
  Let>res2=%res2%%tmp%%CRLF%
EndWhile

MDL>All Sort orders:%CRLF%%res%%CRLF%=============%CRLF%All Sort orders over 8:%CRLF%%res2%

/*
strData:
01_Chapt.mp3
02_Chapt.mp3
Example 2:
001Chapt.mp3
002Chapt.mp3
Example 3:
123456_0_001a_title.mp3
123456_0_002a_title.mp3
Example 4:
dtb-ae123001.mp3
dtb-ae123002.mp3
*/

RNIB
Macro Veteran
Posts: 193
Joined: Thu Jan 10, 2008 10:25 am
Location: London, UK

Re: Find Sort Order and Count Character Position

Post by RNIB » Fri Jul 19, 2024 9:24 am

Ooh Regex, I hadn't thought of that!

I had a very basic understanding of Regex but I've only lightly dabbled with it once in the past and that was several years ago so I definitely need to read up on it a bit more.

I think I can just about follow what your code is doing and will give it a try. I'm just struggling a bit to work out if there are any naming formats that might throw it out but hopefully that will become clearer in time.

Thank you for the suggestion and for the code, I certainly couldn't have done that myself. Very much appreciated.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts