9 Replies Latest reply on Dec 17, 2013 5:05 AM by Unique

    Delete character in file names

    Unique Level 1

      Hi,

       

      I need a help to rename some of the files. i.e. delete some characters of a files.

       

      eg. XX_5182179_ETERNITY_25TH_MB_AB_XYX_abc.ttjs

      eg. XX_4182229_ETERNITY_21TH_MB_AB_XYX_abc.ttjs


       

      like above eg. i have lot of files on a folder in desktop.

       

      and I just want to retain only the blue highlighted text. and remove characters inbetween.  i.e. XX_51312179.ttjs.

       

      I have this script.. but not doing nothing..

       

       

       

      var myDoc = app.activeDocument;

       

      var myPath = myDoc.filePath;

      var myNEWName = (myDoc.name.replace(".indd",".ps"));

      var myDc = new File(myPath+"/"+myNEWName); myDoc.save(myDc);

      myDoc.saveACopy(myDc);

       

       

      Please help me to achieve this..

       

       

      Many thanks

        • 1. Re: Delete character in file names
          pixxxel schubser Level 5

          We need more and more accurate names to set up a defined rule for all cases.

          The replace can do with GREP (or REGEX).

           

           

          For your two (posted) cases you can do something like this:

           

          var myDoc = app.activeDocument; 
          
          var myPath = myDoc.filePath;
          var myNEWName = (myDoc.name.replace(".indd",".ps"));
          // you need only a fixed rule for the characters, which to be replaced, e.g. this works for the two cases
          myNEWName = (myNEWName.replace (/_ETERNITY_..TH_MB_AB_XYX_abc/,""));
          //alert(myNEWName)
          var myDc = new File(myPath+"/"+myNEWName); myDoc.save(myDc);
          //myDoc.saveACopy(myDc);
          

           

           

          Have fun

           

           

           

          But why you save your doc at first and than save the same doc again with „saveAs" ?

          • 2. Re: Delete character in file names
            Unique Level 1

            Hi,

             

            Thanks for your support,

             

            and sorry for the confusion, i made.

             

            Actually, my request is..

             

            I have lot of files inside a folder, which has the file names as

            eg. XX_5182179_ETERNITY_25TH_MB_AB_XYX_abc.ttjs

            eg. XX_5182179_ETERNITY_25TH_MB_AB_XYX_abc.ttjs

            eg. XX_4182111_ETERNITY_21TH_MB_AB_XYX_abc.ttjs etc., (like this 100 files are inside a folder)

             

            I just want to retain only the blue highlighted text and remove orange text in a file name..

             

            the result should be.

             

            i.e. XX_5182179.ttjs

                 XX_5182179.ttjs

             

            Is that possible to run the script from extended tool kit.

            • 3. Re: Delete character in file names
              Mi_D Level 3

              hi Unique,

               

               

              var myfilePath=File("E:\\Work\\rename\\");

              var myFileName = myfilePath.getFiles("*.*");

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

              {

                  myNEWName = (myFileName[i].name.replace (/_ETERNITY_..TH_MB_AB_XYX_abc/,""));

                  myFileName[i].rename(myNEWName);

               

              }

              • 4. Re: Delete character in file names
                Unique Level 1

                Hi Mi_D, 

                 

                Thanks for your support.

                 

                The above script works, only if the file has the same format. like below.

                 

                XX_5182179_ETERNITY_25TH_MB_AB_XYX_abc.ttjs

                 

                 

                The rest of the file name remain unchanged.

                 

                because file names are not in same format..

                 

                varying... like below

                 

                XX_5182000_ETERNITY_25TH_XX_GUIDELINES_SIMPLE_GUIDELINES.ttjs,

                XX_5185555_FER_31ST_AN_123_SIMPLE.ttjs


                SO, we need to retain only the XX and digits and the last .ttjs extension.

                 

                i.e.

                 

                XX_(any digits here).ttjs

                 

                many thanks for the previous and future support.

                • 5. Re: Delete character in file names
                  Unique Level 1

                  Also, im trying to find and replace like this, but this works only if the contents are in indesign.

                   

                   

                  myS = Folder("/Users/wleastudio/Desktop/For Chennai/").getFiles("*"); //remove links from Desktop

                  for ( i = 0; i < myS.length; i++ )

                       { 

                     var nn = myS[i];

                     nt = [];

                     nt.push(nn);

                   

                      app.findGrepPreferences.findWhat = "XX_(\\d+)+_(\\w)+.ttjs";

                      app.changeGrepPreferences.changeTo = "XX_ $1.ttjs";

                  app.changeGrep();

                       }

                   

                   

                  this is not working for a files that is inside a folder on desktop.

                  • 6. Re: Delete character in file names
                    Mi_D Level 3

                    try this

                     

                    var myfilePath=File("E:\\rename\\");

                    var myFileName = myfilePath.getFiles("*.*");

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

                    {

                               myNEWName = (myFileName[i].name.replace (/replaceText/,"hjhj"));

                                myFileName[i].rename(myFileName[i].name.split ("_")[0]+"_"+myFileName[i].name.split ("_")[1]+'.ttjs');

                    }

                    • 7. Re: Delete character in file names
                      Unique Level 1

                      THanks so much Mi_D,

                       

                      Thanks, very much...

                       

                      Unique.

                      • 8. Re: Delete character in file names
                        Laubender Adobe Community Professional & MVP

                        @Unique – so we do not work on InDesign files, but on files ending with .ttjs, right?

                         

                        Are these .ttjs files related to the TouchThing game engine? I ask just out of curiosity…

                         

                        Ok. In your question starting this thread, you are showing an ExtendScript snippet, that is working with the active InDesign file and is doing weird things to it.

                        Did you ever run it in InDesign having a file open?

                         

                        In effect it duplicates the active document giving a new suffix to the duplicate.

                        This is an absolute No-No !! Never rename the suffix of an InDesign file from *.indd to other than *.indt. In your case it was getting the *.ps suffix which usually stands for a PostScript file.

                         

                        Ah. And then, in the last line, that snippet is throwing an error:

                         

                        "Copy of "Test.ps" cannot be saved.

                        File "Test.ps" is already open (error code: 1)."

                         

                         

                        Here an ExtendScript (JavaScript) to rename those .ttjs files (whatever those are).

                        You have to run it from InDesign or from the ESTK (ExtendScript Toolkit).

                         

                        What the script will do:

                         

                        Renaming .ttjs files as you suggested or your contributions here are showing:

                        Leaving alone the "XX_" at the start of the name followed by a digit of variable length.

                         

                        Removing a possible string  beginning with a "_" plus an (nearly) arbitrary part until the suffix is reached.

                         

                        In detail:

                        1. A dialog will prompt you to select one or more files with suffix .ttjs.

                        2. The script will work on every selected file with the right suffix.

                        Provided: the file with the new name does not exist already.

                        3. An alert is showing up when the script is done.

                         

                        The script's action CANNOT be un-done. So be careful!

                        To my tests it does not overwrite existing files on Mac OS.

                        But first test this out carefully, expecially when working on Windows.

                         

                        For scripters: there is a rename() method for the File class.
                        The script is tested on Mac OSX 10.6.8 with InDesign CS5.5.

                         

                        Here the code:

                         

                        //RenamingFiles_Suffix_ttjs_RUN-SCRIPT-FROM-INDESIGN.jsx
                        //Uwe Laubender
                        /**
                        * @@@BUILDINFO@@@ RenamingFiles_Suffix_ttjs_RUN-SCRIPT-FROM-INDESIGN.jsx !Version! Mon Dec 16 2013 17:47:48 GMT+0100
                        */
                        //DESCRIPTION:Tested OK with OSX 10.6.8 with InDesign CS5.5, does not OVERWRITE existing files!
                        
                        var mySelectedFiles= File.openDialog("Select one or some files with suffix \"*.ttjs\":",undefined,true);
                        if(mySelectedFiles === null){alert("Script canceled by user");exit()};
                        
                        var counterSuccess= 0;
                        var counterNoSuccess= 0;
                        var counterWrongFileSuffix = 0;
                        
                        var myFiles = new Array();
                        
                        for(var n=0;n<mySelectedFiles.length;n++){
                            if(mySelectedFiles[n].name.match(/\.ttjs$/)){
                                myFiles.push(mySelectedFiles[n]);
                                };
                            else{++counterWrongFileSuffix;};
                            };
                        
                        for(var n=0;n<myFiles.length;n++){
                        
                            var myFileName = myFiles[n].name;
                        
                            var notNeededString = myFileName.replace(/XX_\d+/,"").replace(/\.ttjs$/,"");
                            var myNewName = myFileName.replace(notNeededString,"");
                        
                            var renamingSuccessful = myFiles[n].rename(myNewName);
                        
                            if(renamingSuccessful){++counterSuccess;};
                            if(!renamingSuccessful){++counterNoSuccess;};
                        
                            };
                        
                        alert("Done!"+"\r"+
                        
                            counterWrongFileSuffix+"\t"+"Files: wrong file suffix"+"\r"+
                            counterNoSuccess+"\t"+".ttjs files not renamed"+"\r"+"\r"+
                        
                            counterSuccess+"\t"+".ttjs FILES RENAMED"+"\r"+"\r"+
                        
                            mySelectedFiles.length+"\t"+"FILES SELECTED"
                        
                            );
                        

                         

                        Since openDialog() and the File class are not unique to InDesign, the script could run successfully from e.g. Adobe Illustrator as well. Maybe all apps that support ExtendScript (not tested).

                         

                        Uwe

                        • 9. Re: Delete character in file names
                          Unique Level 1

                          HI Lau,

                           

                          THanks so much for the great stuff.

                           

                          I'll keep learning from you people for future references.

                           

                          Thanks for the time spent on to make me understand more and better.

                           

                          beer to you..

                           

                          cheers..