I want to rename 660 files using a script I wrote in Javascript – is ‘edit’ the right mode for openn

L
Posted By
lkrubner
Apr 25, 2005
Views
483
Replies
8
Status
Closed
I’ve a friend who needs to rename 660 EPS files. I thought a script would be the way to do it. Trying to help them out, so I’m trying to write a script using Javascript that will allow a string to be added to every file in a folder.

Below you’ll see what I’ve got so far. Is ‘e’ as in ‘edit’ the correct mode to open these files in? Would ‘r’ as in ‘read’ mode be safer and still allow the name change?

var selectedFolder = Folder.selectDialog("Select the folder to run this script on. All files in the folder will be affected.");

var allFilesInFolder = selectedFolder.getFiles();

for (var i=0; i < allFilesInFolder.length; i++) {
var thisFile = allFilesInFolder[i];
var fileOpened = thisFile.open(e);
if (fileOpened) {
var thisFilesName = thisFile.name;
thisFilesName = thisFilesName.replace(".eps",
"_P42_S_W.eps");
thisFile.saveAs(thisFilesName);
thisFile.close();
}

}

How to Master Sharpening in Photoshop

Give your photos a professional finish with sharpening in Photoshop. Learn to enhance details, create contrast, and prepare your images for print, web, and social media.

J
jscheimpflug
Apr 25, 2005
wrote in message
I’ve a friend who needs to rename 660 EPS files.

You don’t really need a script to do that – or is this an academic exercise?
L
lkrubner
Apr 25, 2005
Can this be done with Actions?

Also: I hope to generalize and allow a variable to take any string for filename changes.
H
harrylimey
Apr 25, 2005
wrote in message
Can this be done with Actions?

Also: I hope to generalize and allow a variable to take any string for filename changes.

Not sure how easy it would be to do this in PS, but I suggest you try Irfanview – a free utility that is very good at this sort of thing – and very fast!!
http://www.irfanview.com/

Harry
J
jjs
Apr 25, 2005
wrote in message
Can this be done with Actions?

Also: I hope to generalize and allow a variable to take any string for filename changes.

I don’t understand the second part. Check the CS Browser. Start photoshop. File->Browse. Find the directory of files. Use Automate->Batch Rename.
Check out the dialog box for renaming options.

Or try IrfanView
Get if from: http://www.irfanview.com/main_download_engl.htm There is a Batch option which includes Batch Rename.
See the patterns option.
L
lkrubner
Apr 25, 2005
Thank you. Does Batch Rename allow one to subtract strings from a filename? It seems to be an easy way to add to filename, but not to pull things from it. I might need my script to subtract things from a file name?
L
lkrubner
Apr 29, 2005
Okay, I wrote a script that can subtract strings from a file name. Combined with Photoshop’s ability to add to file names, this is useful when you have to constantly have to remain name thousands of filenames to indicate what stage of the production process they are in (this is something we do a lot of where I work).

I’ll add this to the tutorial I have up here:

http://www.geocities.com/lawrence_k.geo/

Script below.

======================================

// This first line simply creates a variable where we will, later on, store the names
// of the files whose names we change. We do this so that at the end of the script we
// can say, hey, we changed these names.
var filesRenamed = "We changed the names of these files: ";

// This next line causes a dialog box to open on screen and ask the // user to choose a folder. Every file in the folder will be effected // by this script.
var selectedFolder = Folder.selectDialog("Select the folder to run this script on. All files in the folder will be affected.");

// This next line makes sure that we really do have a reference to a folder.
// The user might cancel the operation at this point, and if so, selectedFolder will
// be empty, in which case there is no point going on with the script. if (selectedFolder instanceof Folder) {

// what should be removed from the file name?
// If you have a file name like "P42_WTTI98_Ochre_Settings.psd" and // you want to get rid of "_Settings" then in this dialog box, when // it opens on screen, you’d type "_Settings". var stringToBeRemoved = prompt("What portion of the file name do you wan to remove (example: ‘_p52’ to remove ‘_P52’ from the file name)", "Type the letters or numbers you’d like removed from file names.");

// The replace command, down below, uses a special variable called // a "regular expression". This is created through the special command // RegExp. The next line basically formats the stringToBeRemoved in a way
// that allows the replace command to understand it. We need to create this
// variable here, instead of down below, because here we can create this
// variable just one time, whereas down below we would be creating it again
// for ever file we intend to change. This is more efficient. regEx = new RegExp(stringToBeRemoved, "g");

// This next line checks to see that the user actually typed something in. If they
// didn’t then there is no point proceeding.
if (stringToBeRemoved != "") {

// The variable selectedFolder is now a reference pointing at the folder we want to effect.
// The getFiles command returns a list of every file in the folder. var allFilesInFolder = selectedFolder.getFiles();

alert("This folder contains this many files: " + allFilesInFolder.length);
// This next line is a special line that causes the script to loop through the list
// of files one at a time. All the lines below are then done to each file, one at a time.
for (var i=0; i < allFilesInFolder.length; i++) {

// This next lines gets a reference to one particular file from the list.
// Each time we go through the loop, the next file in the list will be choosen.
var thisFile = allFilesInFolder[i];

// This next lines gets the name of the file and stores the name in the
// variable called oldName.
var oldName = thisFile.name;

// It’s important that you don’t erase the entire filename. The file might
// disappear if you erase the entire name. This next line makes sure that
// you’re not erasing the whole file name. This next line basically says
// "Proceed only if the letters and numbers to be removed don’t equal the
// whole file name."
if (stringToBeRemoved != oldName) {

// This next line takes whatever value is in stringToBeRemoved and replaces
// it with nothing. Thus the string is removed from the name. However, we
// have not yet changed the file name, we’ve just created a variable called
// newName that has stored the name we’d like the file to have. var newName = oldName.replace(regEx,"");

// This next line is the one that actually changes the file name. We use
// the rename command.
thisFile.rename(newName);

// This next line checks what we just done. File name should now equal the
// new name. If it doesn’t, then something went wrong.
if (thisFile.name != newName) {
alert("Unable to rename " + thisFile.absoluteURI + "->" + newName);
} else {
filesRenamed += newName + ", ";
}
} else {
alert("Sorry, but what you typed is the same as the name of the file. You’d erase the whole file name! Very bad!"); }
}
} else {
alert("Sorry, but you have to type in something for us to remove it from the filenames.");
}
} else {
alert("Sorry, but you didn’t select a folder."); }

alert("The script is done now." + filesRenamed);
J
jjs
Apr 29, 2005
wrote in message
Okay, I wrote a script that can subtract strings from a file name.

🙂 Cool. IrfanView can do the same thing. Just enter the string to find, and nothing in the ‘replace’ window.
R
RSD99
May 24, 2005
Yes.

From the web site:

"IrfanView is a very fast, small, compact and innovative FREEWARE (for non-commercial use) graphic viewer for Windows 9x/ME/NT/2000/XP/2003."

Windows Only.

wrote in message
Am I mistaken, or is Irfan only for Windows? I work with a Mac-only shop.

MacBook Pro 16” Mockups 🔥

– in 4 materials (clay versions included)

– 12 scenes

– 48 MacBook Pro 16″ mockups

– 6000 x 4500 px

Related Discussion Topics

Nice and short text about related topics in discussion sections