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

[JS] rename files without opening in PS

Explorer ,
Nov 23, 2018 Nov 23, 2018

Copy link to clipboard

Copied

Thank you guys for your helps.

I have one more question:

How can I rename file(s) by script? is there anyway to rename a file without open it in Photoshop?

for example:

I have these files in a folder:

1Wh-1.jpg

1Cl-3.jpg

1Gy-5.jpg

(these names are fix and my Action always save file by these name)

and want to rename them to:

1Wh-13005-1.jpg

1Cl-10810-3.jpg

1Gy-10420-5.jpg

[ moved from using top layer name in save as for web and generate file name  by moderator ]

[ one question - some answers - maybe one correct answer   |  but not dozens of questions in endless threads]

[ One note:

If there is a correct answer, please mark this answer as 'Correct Answer' (and not as 'Assumed Answered')

If there are helpful answers, you can mark them as 'Helpful Answer'.

Thanx ]

TOPICS
Actions and scripting

Views

4.8K

Translate

Translate

Report

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 , Nov 23, 2018 Nov 23, 2018

var aFile = File("~/Desktop/TestText.txt");

aFile.rename ("NewName.txt");

Have fun

Votes

Translate

Translate
Adobe
Community Expert ,
Nov 23, 2018 Nov 23, 2018

Copy link to clipboard

Copied

As far as I know, the only way is to copy the file with a new name, then delete the old file.

var oldFile = new File('/c/myFile.jpg');

oldFile.copy('/c/newFileName.jpg');

oldFile.remove();

Votes

Translate

Translate

Report

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 ,
Nov 23, 2018 Nov 23, 2018

Copy link to clipboard

Copied

var aFile = File("~/Desktop/TestText.txt");

aFile.rename ("NewName.txt");

Have fun

Votes

Translate

Translate

Report

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
New Here ,
Jul 03, 2020 Jul 03, 2020

Copy link to clipboard

Copied

LATEST

this does nothing on my side. I am trying to rename the current document based on a layer's content. Is this right?

 

var Doc = app.activeDocument;
Doc.activeLayer = Doc.layers.getByName("DMP TEXT");
var text_content = Doc.activeLayer.textItem.contents;
var extension = Doc.name.split('.').pop();
var sep = String (".");
var newName = Doc.path.fsName + "\\" + text_content + sep + extension;

var thisFile = File(Doc.path.fullName);
thisFile.rename(text_content + sep + extension);

 

Votes

Translate

Translate

Report

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 ,
Nov 23, 2018 Nov 23, 2018

Copy link to clipboard

Copied

Using Adobe Bridge, it would be similar to my previous reply in the other topic thread:

Re: using top layer name in save as for web and generate file name

However your filename pattern has now changed, so the find/replace would need to be adjusted to use a regular expression… So:

Find: (^.+-)(.+)

Replace: $113005-$2

(there are many valid regular expressions, the example above is very “loose” and if there were always going to be a fixed pattern such as a leading digit followed by two alphabetical then a hyphen then the expression could be made “tighter")

bridge-batch-rename-regex.png

JavaScript can also use Regular Expressions...

Targeting a hard-coded file path/file:

#target photoshop

var aFile = File('~/Desktop/1Wh-1.jpg'); 

aFile.rename( aFile.name.replace(/(^.+-)(.+)/, '$113005-$2') ); 

alert("File renamed")

Or using a hard-coded input folder path, filtering on all files with a .jpg extension:

#target photoshop

var inputFolder = Folder ( '~/Desktop/' ); 

var fileList = inputFolder.getFiles( '*.jpg' ); 

for ( var i = 0; i < fileList.length; i++ ) { 

fileList.rename( fileList.name.replace( /(^.+-)(.+)/, '$113005-$2') );    

};

alert('Matching .jpg files in Desktop folder renamed')

Or using a dialog to select the source folder containing .jpg or .JPG files, rather than hard-coding it:

#target photoshop

var inputFolder = Folder.selectDialog("Select a folder containing .jpg files to process"); 

var fileList = inputFolder.getFiles(/\.jpg$/i); 

for ( var i = 0; i < fileList.length; i++ ) { 

fileList.rename( fileList.name.replace( /(^.+-)(.+)/, '$113005-$2') );    

};

alert('Matching .jpg files in target folder renamed')

The .getFiles extension mask can of course be expanded for other file types:

.getFiles(/\.(jpg|tif|psd)$/i);

Or using no file extension mask .getFiles();

I’m still trying to figure out how to create a GUI dialog to populate a variable for the replacement text, rather than hard coding it in.

Votes

Translate

Translate

Report

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 ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

dc-master  schrieb

… How can I rename file(s) by script? is there anyway to rename a file without open it in Photoshop?

for example:

I have these files in a folder:

1Wh-1.jpg

1Cl-3.jpg

1Gy-5.jpg

(these names are fix and my Action always save file by these name)

and want to rename them to:

1Wh-13005-1.jpg

1Cl-10810-3.jpg

1Gy-10420-5.jpg

Do you alwas have this three file names?

And do you always want to rename with the same numbers in between?

Or is there a rule for your file names?

eg: [digit][letter[letter][-][digit][.][jpg]

And in which case which number should be pushed in between?

Only a study for more security:

check if your jpg(s) follows this name convention and try (files with other names are not affected):

//var inputFolder = Folder.selectDialog("Select a folder containing .jpg files to process");

var inputFolder = Folder("~/Desktop/TestFolder/Rename");

var aFile = null;

var nameParts = null;

var fileList = inputFolder.getFiles ( /\.jpg$/i );

for ( var i = 0; i < fileList.length; i++ ) {

    aFile = fileList;

    if (aFile.name.match ( /^\d[A-Z][a-z]-\d\./ ) != null ) {

        nameparts = aFile.name.split("-");

        if (nameparts[0].search ( "1Wh" ) != -1) {

            nameparts.splice(1, 0, "13005")

            fileList.rename (nameparts.join("-"));

        };

        if (nameparts[0].search ( "1Cl" ) != -1) {

            nameparts.splice(1, 0, "10810")

            fileList.rename (nameparts.join("-"));

        };

        if (nameparts[0].search ( "1Gy" ) != -1) {

            nameparts.splice(1, 0, "10420")

            fileList.rename (nameparts.join("-"));

        };

    };

};

alert ( 'Done' );

Votes

Translate

Translate

Report

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
Explorer ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

there are some rules for my file names:

For example:

1Wh-1.jpg

First Character will be always a number between 1 to 9

second and third character will be one of these "Wh", "Gy", "Bl", "Cl", Nb" or "Mg"

and at the end will be "-1" to "-9"

(I did this, because I'm making these JPG files by using Export --> Save for Web in Actions. And in Photoshop Actions I couldn't use dynamic file names filename and it export by filenames that you entered on recording time)

I want to add a Top layer name from current document  (or if you have suggestion to enter a text easier by better way) in the middle of these file names.

therefore after 3rd character will add the text and after that will be 4th and 5th character.

like 1Wh_TopLayer-1.jpg

here I write this script:

//===== Read folder address of JPG files from a text file ====
var a = new File("~/DC-Game-Folder.txt");

a.open('r');

var docpath = "";

docpath = a.readln();

a.close();

var PSDdoc = app.activeDocument;

var DesignName = PSDdoc.layers[0].name;

//====================

// =====  Picture 1 ======

var PicNum=1;

var JPGdoc = File(docpath + "/1Wh-" + PicNum +".jpg");

if(JPGdoc.exists) {

    JPGdoc.rename("1Wh_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

var JPGdoc = File(docpath + "/1Gy-" + PicNum +".jpg");

if(JPGdoc.exists) {

JPGdoc.rename("1Gy_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

var JPGdoc = File(docpath + "/1Bl-" + PicNum +".jpg");

if(JPGdoc.exists) {

JPGdoc.rename("1Bl_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

var JPGdoc = File(docpath + "/1Cl-" + PicNum +".jpg");

if(JPGdoc.exists) {

JPGdoc.rename("1Cl_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

var JPGdoc = File(docpath + "/1Mg-" + PicNum +".jpg");

if(JPGdoc.exists) {

JPGdoc.rename("1Mg_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

var JPGdoc = File(docpath + "/1Nb-" + PicNum +".jpg");

if(JPGdoc.exists) {

JPGdoc.rename("1Nb_" + DesignName + "-" + PicNum + ".jpg");

}

var JPGdoc = "";

then repeat for "2" at the end and "3" and ....

here is a sample of some files after rename:

Screen Shot 2018-11-24 at 10.50.57 PM.png

Votes

Translate

Translate

Report

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 ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

dc-master  schrieb

… I want to add a Top layer name from current document  (or if you have suggestion to enter a text easier by better way) in the middle of these file names.

therefore after 3rd character will add the text and after that will be 4th and 5th character.

like 1Wh_TopLayer-1.jpg …

This is, what my script snippet does. Did you tried it?

One futher question:

Where the top level layers name comes from?

(And image: 1KB_133-07_Deathpool.jpg does not match the rule.)

Votes

Translate

Translate

Report

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
Explorer ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

This is, what my script snippet does. Did you tried it?

I didn't tried because I was not on my computer. will try next week.

One futher question:

Where the top level layers name comes from?

I change Top layer name manually and type design code and name.

(And image: 1KB_133-07_Deathpool.jpg does not match the rule.)

also yes!
few files have exception and I rename those manually.

Votes

Translate

Translate

Report

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 ,
Nov 25, 2018 Nov 25, 2018

Copy link to clipboard

Copied

While I read your last postings again:

IMO it is NOT POSSIBLE with 'normal' ExtendScript to get the top level layers name WITHOUT OPENING the file.

Votes

Translate

Translate

Report

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
Explorer ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

thanks for your help

I know that

Actually I'm using a PSD file and use activeLayer.name as the middle text to rename JPG files.

my PSD file:

Screen Shot 2018-12-14 at 4.10.11 PM.png

The JPG files before rename: (I create them by Photoshop actions)

Screen Shot 2018-12-14 at 4.09.12 PM.png

The JPG files after rename:

Screen Shot 2018-12-14 at 4.10.36 PM.png

Votes

Translate

Translate

Report

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
Explorer ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

sorry I'm not very familiar by JavaScript.

pixxxel schubser

May you explain me more about this section that I can change them as I like.

I didn't understand RED parts

var fileList = inputFolder.getFiles ( /\.jpg$/i )

for ( var i = 0; i < fileList.length; i++ ) { 

    aFile = fileList

if (aFile.name.match ( /^\d[A-Z][a-z]-\d\./ ) != null ) { 

nameparts = aFile.name.split("-"); 

  if (nameparts[0].search ( "1Wh" ) != -1) { 

    nameparts.splice(1, 0, "13005"

   fileList.rename (nameparts.join("-")); 

  }; 

Votes

Translate

Translate

Report

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 ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

https://forums.adobe.com/people/pixxxel+schubser  schrieb

dc-master   schrieb

… How can I rename file(s) by script? is there anyway to rename a file without open it in Photoshop?

for example:

I have these files in a folder:

1Wh-1.jpg

1Cl-3.jpg

1Gy-5.jpg

(these names are fix and my Action always save file by these name)

and want to rename them to:

1Wh-13005-1.jpg

1Cl-10810-3.jpg

1Gy-10420-5.jpg

Do you alwas have this three file names?

And do you always want to rename with the same numbers in between?

Or is there a rule for your file names?

eg: [digit][letter[letter][-][digit][.][jpg]

And in which case which number should be pushed in between?

Only a study for more security:

check if your jpg(s) follows this name convention and try (files with other names are not affected):

//var inputFolder = Folder.selectDialog("Select a folder containing .jpg files to process"); var inputFolder = Folder("~/Desktop/TestFolder/Rename"); var aFile = null; var nameParts = null;  var fileList = inputFolder.getFiles ( /\.jpg$/i ); for ( var i = 0; i < fileList.length; i++ ) {     aFile = fileList;     if (aFile.name.match ( /^\d[A-Z][a-z]-\d\./ ) != null ) {         nameparts = aFile.name.split("-");         if (nameparts[0].search ( "1Wh" ) != -1) {             nameparts.splice(1, 0, "13005")             fileList.rename (nameparts.join("-"));         };         if (nameparts[0].search ( "1Cl" ) != -1) {             nameparts.splice(1, 0, "10810")             fileList.rename (nameparts.join("-"));         };         if (nameparts[0].search ( "1Gy" ) != -1) {             nameparts.splice(1, 0, "10420")             fileList.rename (nameparts.join("-"));         };     }; }; alert ( 'Done' );

What do you want to change?

All cases could be added with additional if clauses like:

(But you also could write a function for this.)

if (nameparts[0].search ( "1Gy" ) != -1) {

    nameparts.splice(1, 0, "10420")

    fileList.rename (nameparts.join("-"));

};

For nameparts with 1BL change line #1 in this example to

   if (nameparts[0].search ( "1BL" ) != -1) {

You want add eg 45678 inbetween the filename - change line#2 in the previous if clause to:

    nameparts.splice(1, 0, "45678")

-------------------------------------------

Your questions:

var fileList = inputFolder.getFiles ( /\.jpg$/i ); // find JPG images only
if (aFile.name.match ( /^\d[A-Z][a-z]-\d\./ ) != null ) { // find [digit][letter[letter][-][digit][.] in file name

nameparts.splice(1, 0, "10420") // insert the new number at position 1 in the array nameparts

Have fun

Votes

Translate

Translate

Report

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
Explorer ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

Thanks pixxxel schubser​​ and sorry to ask more

actually I didn't understand syntax of "( /\.jpg$/i )"

in this line : var fileList = inputFolder.getFiles ( /\.jpg$/i )

also I'm getting  error when use that and changed to ("*.jpg")

if (aFile.name.match ( /^\d[A-Z][a-z]-\d\./ ) != null )

what is !=null

and also

if (nameparts[0].search ( "1Wh" ) != -1)

What is !=-1

Votes

Translate

Translate

Report

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 ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

Which OS? MacOS?

Which error message?

match ... != null

means a positiv hit (is not null)

This method returrns an array   or   nothing (null)

search ... != -1

means the same, a positiv hit (is not - 1)

returns a position of a positiv match   or   no position (-1)

Votes

Translate

Translate

Report

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
Explorer ,
Dec 14, 2018 Dec 14, 2018

Copy link to clipboard

Copied

now I'm working on Mac but the script have to work on Mac and Windows (because we are using 2 computers)

no error message but ExtendScript toolkit stop when reach to this line

Votes

Translate

Translate

Report

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