Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Batch Rename String Substitution

Community Beginner ,
Sep 29, 2021 Sep 29, 2021

I am currently looking to rename/rearrance a file name as a batch but I am not familiar with Reg Ex to know what to use for string substitution. So here is my delima. I have file names with similar structure. Below is original filename and then what I would like for it to be. Any help would be greatly appreciated. 

 

Orginal Filename structure:

A-1LastnameFirstname1211.pdf

 

Wanted Result:

Lastname, Firstname [1211].pdf

 

The number at end will be different for each file name.

TOPICS
Scripting
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 29, 2021 Sep 29, 2021

Try the following regular expression find/replace:

 

Find:

(^.{3})([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

or

 

(^.+-\d+)([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

or

 

(^.+\d+)([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

Replace:

$2, $3 [$4]$5

 

 

regex.pngexpand image

Translate
Community Expert ,
Sep 29, 2021 Sep 29, 2021

Try the following regular expression find/replace:

 

Find:

(^.{3})([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

or

 

(^.+-\d+)([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

or

 

(^.+\d+)([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)

 

Replace:

$2, $3 [$4]$5

 

 

regex.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2021 Sep 30, 2021

Stephen, you're awesome! This appears to work with the file naming formats I'm having to work with and should save me a lot of time that I don't have trying to figure out RegEx syntax. I greatly appreciate it!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 30, 2021

@sportshooter â€“ Your welcome!

 

Are you using this regex with the Batch Rename or in a script?

 

As you can see from my three examples and the one from @Kukurykus there are different equivalent regex searches that may or may not work depending on how your data may vary. You need to know your own data! It is very hard to write a robust or bullet proof regex with only a sinlge example pattern, as there will likely be exceptions/variances. One can write a very "tight" search pattern, or make it more flexible.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 30, 2021 Sep 30, 2021

I'm using it witht he Batch Rename. For this particular instance, the file naming scheme should stay constant with no variations. This is more or less a process that only I would use as part of an internal process. I understand that I should know this myself and tried several resources on the syntax to try to figure out on my own but due to time constraints reached out in this group to hopefully find a solution. I will be looking at what you provided to try to understand the syntax on my own but not having a programming background to easily comprehend, it will take me some time to get there. This is a process I will only be doing once a month which makes it even more difficult to understand when I'm not doing on a regular basis. I truly appreciate your assistance and advice on this! 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 30, 2021

When I said "know your own data", a pattern match may break if it is expecting say exactly 4 digits  \d{4}  but the data may "unexpectedly" vary and have more or less digits than originially expected. With only a single example, one can be exact, or offer a little bit of leeway where things may possibly vary, as you can see in the screenshot below.

 

As the Bridge regular expression fields are so terrible in later versions, it is helpful to write and test the expression in a better tool, such as:

 

regular expresions 101

RegExr

RegEx Pal

Or many other similar resources.

 

Use a PCRE compatable engine (not JavaScript etc) and put in your filename test string and use the find/replace options to test the code offered previously. There is usally an option to "translate" the code so that you can understand what is taking place (the right hand side of the screenshot below).

 

Regular expressions are a language, I get it, like any other "second language" if you don't use it regularly, you lose it.

 

regex101.pngexpand image

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 29, 2021 Sep 29, 2021
'A-1LastnameFirstname1211.pdf'.replace(/(.{3})([A-Z]\w*)([A-Z]\w*)(\d{4})/g, '$2,$3 [$4]')
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2021 Sep 29, 2021

I didn't see the scripting tag, so assumed that this would be a simple Batch Rename task.

 

@Kukurykus â€“ just to be clear, that is just code snippet, yes?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

Yes, the user should use it for activeDocument.activeLayer.name

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 30, 2021

That would be within Photoshop, yes?

 

In a Bridge script, I believe that it would be different (the selected thumb name)... Anyway, unless this was part of a larger Bridge automation script, I can't think why one would not use Batch Rename and instead "recreate the wheel".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

Ah right, the Bridge! Then for visible thumbnails use:

 

(thmbnls = app.document.visibleThumbnails); while(thmbnls.length) (shft = thmbnls.shift())
.spec.rename(shft.name.replace(/(.{3})([A-Z]\w*)([A-Z]\w*)(\d{4})/g, '$2,$3 [$4]'))

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 30, 2021 Sep 30, 2021

I hacked this:

 

/*
https://community.adobe.com/t5/bridge-discussions/batch-rename-string-substitution/td-p/12413178
Batch Rename String Substitution
Based on - https://www.ps-scripts.com/viewtopic.php?f=72&t=14282&p=89792&hilit=rename#p89787

Orginal Filename structure:
A-1LastnameFirstname1211.pdf

Wanted Result:
Lastname, Firstname [1211].pdf
*/

#target bridge
if (BridgeTalk.appName == "bridge") {
   regexRename = new MenuElement("command", "Regular Expression Rename", "at the end of tools");
}
regexRename.onSelect = function () {

   var sels = app.document.selections;

   for (var z = 0; z < sels.length; z++) {
      var thumb = sels[z];
      var selectedFile = thumb.spec;
      thumbName = selectedFile.name.replace(/(^.+\d+)([A-Z].+?)([A-Z].+?)(\d+)(\.[^\.]+$)/g, '$2, $3 [$4]$5');
      File(selectedFile).rename((thumbName));
   }
}

 

However, I would like to store the original filename before the rename. The following snippet works when used as above for the rename, however I can't combine both into a single script without Bridge returning an error that it can't write the PreservedFileName metadata. I have tried in a single for loop and as two separate for loops etc.

 

for (var i = 0; i < sels.length; i++){
var md = sels[i].synchronousMetadata;
    md.namespace = "http://ns.adobe.com/xap/1.0/mm/";
    var Name = decodeURI(sels[i].name).replace(/\.[^\.]+$/, '');
    md.PreservedFileName = md.PreservedFileName + Name;
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2021 Sep 30, 2021

Put it at end of the code from my previous post (however I tried it for jpgs):

 

&& (md = (thmbnl = new Thumbnail(File(shft.parent.spec + '/' + nme))).synchronousMetadata, md.namespace = 'http://ns.adobe.com/xap/1.0/mm/', md.PreservedFileName = shft.name)

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 24, 2022 Mar 24, 2022

I'm trying to figure out getting rid of the _0001_ sequence number before the Filename of files such as:

_0002_CatInTheHat.jpg

 

What would be the string substitution I would need get rid of the _0002_ where the 2 is sequential up to double digits?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2022 Mar 24, 2022

 

[_\d]+ // Original Filename and only Use Regular Expression

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 24, 2022 Mar 24, 2022
LATEST

Excellent. Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines