Pad Number and Dates

Bulk Rename Utility How-To's

Pad Number and Dates

Postby play » Wed Jun 03, 2020 2:14 am

I have been using Bulk Rename Utility for a few days now and have helped me accomplish so much but I still have trouble with RegEx. I'm hoping someone will be able to assist. I can't find anything on the web with a solution.

I need to rename folders and files to have a certain type of padding before the number and dates.
The basic idea is "5digitnumber-year-month-day-name" in the format of #####-####-##-##-name where x is representative of unknown dates.

My current file structure is as follows:

Folder 1
589
589-1919-6-2-name...
589-2018-7-2-name...
589-2018-12-20-name...
589-xxxx-xx-xx-name...
589-2019-xx-xx-name
1568
1568-2019-6-12-name
etc.
10146
10146-2019-10-xx-name
etc.
Folder 2
etc.

What I need the file structure to look like

Folder 1
00589
00589-1919-06-02-name...
00589-2018-07-02-name...
00589-2018-12-20-name...
00589-xxxx-xx-xx-name...
00589-2019-xx-xx-name
01568
01568-2019-06-12-name
etc.
10146
10146-2019-10-xx-name
etc.
Folder 2
etc.


I have examined the forum and the Bulk Rename Utility Operations Manual (PDF) without any luck.
play
 

Re: Pad Number and Dates

Postby RegexNinja » Wed Jun 03, 2020 8:51 am

With regex, there's no way to specify a conditional number-of-zero's in the replacement.
Often, the best you can do is to run a regex several times until achieving your goal.

Run1 Fix single-digit months:
^(\d{1,5}-)((?:19|20)\d{2}-)([1-9]-\d{1,2}-.*)$
\1\20\3

Run2 Fix single-digit days:
^(\d{1,5}-)((?:19|20)\d{2}-\d{2}-)([1-9]-.*)$
\1\20\3

Run3 Prefix 1-zero to names starting with 1-4Digits (if digits are at EndName, or followed by -yyyy-dd-mm).
For names starting with only 1Digit, you'd have to run this 4-times to achieve the 5Digit format.
^(\d{1,4})($|-(?:19|20)\d{2}-\d{2}-\d{2}-.*)$
0\1\2

With the paid version, javascript fixes everything in 1-run:
if (/^\d{1,5}($|-(19|20)\d{2}-\d{1,2}-\d{1,2})-.*/.test(name)) {
a=name.split('-')
a[0]=('0000'+a[0]).slice(-5)
a[2]=('0'+a[2]).slice(-2)
a[3]=('0'+a[3]).slice(-2)
newName=a.join('-')}
if (/^\d{1,4}$/.test(name)) {newName=('0000'+name).slice(-5)}
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Pad Number and Dates

Postby play » Wed Jun 03, 2020 11:42 pm

Thanks for all the help, How would we fix files that have missing data and are labeled with an "x". Such as the following.

Before
435-1914-1-xx-Plans-Record Drawing-Original Construction.pdf
435-1914-xx-xx-Plans-Record Drawing-Original Construction.pdf
435-xxxx-xx-xx-Plans-Record Drawing-Original Construction.pdf
435-xxxx-xx-1-Plans-Record Drawing-Original Construction.pdf
7635-xxxx-xx-xx-Plans-Record Drawing-Original Construction.pdf


After (What I Need)
00435-1914-01-xx-Plans-Record Drawing-Original Construction.pdf
00435-1914-xx-xx-Plans-Record Drawing-Original Construction.pdf
00435-xxxx-xx-xx-Plans-Record Drawing-Original Construction.pdf
00435-xxxx-xx-01-Plans-Record Drawing-Original Construction.pdf
07635-xxxx-xx-xx-Plans-Record Drawing-Original Construction.pdf

This may also require multiple runs to complete. I assume that we would just change the character classes to [/s/S]. All of my edits that I try to make will change files that already have the correct naming. For the files with the "x" it may be best to work in reverse order and do the following.

Run4 Prefix 1-zero Run this multiple times until 5 characters exist
Run5 Fix single-digit months and ensure that 2 characters exist
Run6 Fix single-digit days and ensure that 2 charters exist

The year should already have all 4 characters.
play
 

Re: Pad Number and Dates

Postby play » Wed Jun 03, 2020 11:50 pm

Not all the files are named -Plans-Record Drawing-Original Construction.pdf, just an example if it matters.
play
 

Re: Pad Number and Dates

Postby play » Thu Jun 04, 2020 1:05 am

I also have a license if javascript is easier using if statements
play
 

Re: Pad Number and Dates

Postby RegexNinja » Thu Jun 04, 2020 8:02 am

Hi.. Since you've got the paid version, dont bother with the regexes.
They're meant for names with only-numbers in the month/day (when month/day exists).
I assumed by: "#####-####-##-##-name where x is representative of unknown date", you meant x = unknown-number?

Unfortunately, many people use x to represent numbers/etc, so I need to ask:
Does x represent a literal 'x', or can it represent 'some-other-character'?
I'll post updated regexes/javascript, once I understand the OrigNames.

Also, what should NewNames be for names like: -Plans-Record Drawing-Original Construction.pdf ??
Prefix with 00000-?? or maybe something like 00000-xxxx-xx-xx-??
Lastly, should anything happen with names like: -1998-06-24-AnyText or 1998-06-24-AnyText ??
Cheers!
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm

Re: Pad Number and Dates

Postby play » Fri Jun 05, 2020 12:34 am

x represent a literal 'x'. This was used when the documents did not include a date. After consider, I will probably just replace all the literal 'x' with 0 for documentation that do not have dates. I still have to organize the files using a different program. I do not believe that I need any additional help and wanted to that you for your support.
play
 

Re: Pad Number and Dates

Postby RegexNinja » Fri Jun 05, 2020 5:16 am

Gotcha, if years like 20xx are possible, you might wanna stay with xx versus 00 ??
Here's the modded expressions to handle either digits or a literal 'xx'.

Run1 Fix single-digit months:
^(\d{1,5}-)((?:19|20|xx)(?:\d{2}|xx)-)([1-9]-(\d{1,2}|xx)-.*)$
\1\20\3

Run2: Fix single-digit days:
^(\d{1,5}-)((?:19|20|xx)(?:\d{2}|xx)-(?:\d{2}|xx)-)([1-9]-.*)$
\1\20\3

Run3: Prefix 1-zero to names starting with 1-4Digits (if digits are at EndName, or followed by -####-##-##, -xxxx/etc).
Run multiple-times until achieving the 5Digit format.
^(\d{1,4})($|-(?:19|20|xx)(?:\d{2}|xx)-(?:\d{2}|xx)-(?:\d{2}|xx)-.*)$
0\1\2


Javascript (does everything above):
if (/^\d{1,5}($|-(19|20|xx)(\d{2}|xx)-(\d{1,2}|xx)-(\d{1,2}|xx))-.+/.test(name)) {
a=name.split('-')
a[0]=('0000'+a[0]).slice(-5)
a[2]=('0'+a[2]).slice(-2)
a[3]=('0'+a[3]).slice(-2)
newName=a.join('-')}
if (/^\d{1,4}$/.test(name)) {newName=('0000'+name).slice(-5)}

Conditions:
Names must begin with 1-5Digits.
If any text follows, it must be in your date-format: -####-##-##-
The 1st-2 ## must be either 19|20|xx, all other ##'s must be Any2Digits or xx.
If date-format exists, then at least 1-char should follow (like Plans-Record)

To make the 1-5Digit prefix optional, change {1,5} --> {0,5}
That mandates names beginning with either: 1-5Digits or your date-format.
You'd also need to change '0000' --> '00000' (to prefix 00000 to date-formats without a prefix).


Results:
01 ----------------------------------> 00001
1-19xx-7-1-anyname -------------> 00001-19xx-07-01-anyname
435-1914-1-xx-Plans-Record ----> 00435-1914-01-xx-Plans-Record
435-1914-xx-xx-Plans-Record ---> 00435-1914-xx-xx-Plans-Record
435-xxxx-xx-1-Plans-Record ----> 00435-xxxx-xx-01-Plans-Record
435-xxxx-xx-xx-Plans-Record ---> 00435-xxxx-xx-xx-Plans-Record
589-1919-06-2-name1 -----------> 00589-1919-06-02-name1
589-1919-06-02-name2 ----------> 00589-1919-06-02-name2
589-1919-6-2-name3 -------------> 00589-1919-06-02-name3
7635-xxxx-xx-xx-Plans-Record --> 07635-xxxx-xx-xx-Plans-Record
-AnyName -------------------------> No effect (must begin with 1-5Digits, then date-format)
-19xx-6-2-AnyName --------------> No effect (same as above)

Optional Results:
Use the edits: '00000' and {0,1} to achieve the exact same results except for last example:
-19xx-6-2-AnyName --------------> 00000-19xx-06-02-AnyName
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm


Return to How-To