I want to rename 660 files using a script I wrote in Javascript - is 'edit' the right mode for openn

Discussion in 'Photoshop Tutorials' started by lkrubner, Apr 25, 2005.

  1. lkrubner

    lkrubner Guest

    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;
    var fileOpened = thisFile.open(e);
    if (fileOpened) {
    var thisFilesName = thisFile.name;
    thisFilesName = thisFilesName.replace(".eps",
    "_P42_S_W.eps");
    thisFile.saveAs(thisFilesName);
    thisFile.close();
    }

    }
     
    lkrubner, Apr 25, 2005
    #1
    1. Advertisements

  2. lkrubner

    jjs Guest

    You don't really need a script to do that - or is this an academic exercise?
     
    jjs, Apr 25, 2005
    #2
    1. Advertisements

  3. lkrubner

    lkrubner Guest

    Can this be done with Actions?

    Also: I hope to generalize and allow a variable to take any string for
    filename changes.
     
    lkrubner, Apr 25, 2005
    #3
  4. lkrubner

    Harry Limey Guest

    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
     
    Harry Limey, Apr 25, 2005
    #4
  5. lkrubner

    jjs Guest

    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.
     
    jjs, Apr 25, 2005
    #5
  6. lkrubner

    lkrubner Guest

    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?
     
    lkrubner, Apr 25, 2005
    #6
  7. lkrubner

    lkrubner Guest

    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;

    // 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);
     
    lkrubner, Apr 29, 2005
    #7
  8. lkrubner

    jjs Guest

    :) Cool. IrfanView can do the same thing. Just enter the string to find, and
    nothing in the 'replace' window.
     
    jjs, Apr 29, 2005
    #8
  9. lkrubner

    lkrubner Guest

    Am I mistaken, or is Irfan only for Windows? I work with a Mac-only
    shop.
     
    lkrubner, May 23, 2005
    #9
  10. lkrubner

    RSD99 Guest

    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.
     
    RSD99, May 24, 2005
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.