Find and Save files - Exact match only - CSV

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Limey

Find and Save files - Exact match only - CSV

Post by Limey »

Hi folks...
Here is one of the first scripts I ever got help with (Ok, help really isn't fair as it was completely made for me!). What' happening is that it's pulling files that are not in the list. I'm not sure what prompted those sku numbers to be included.
Below is the script and a list of the files pulled and what were requested in the CSV. Could someone take a look at it to see if it can be tightened to just pull exact matches to the entries in the CSV, column A?

Note: I have just run a test where I removed the other columns in the CSV file and it did just find the files requested. It seems as if the script is searching throughout the document and pulling other numbers. The strange thing is that the numbers it's grabbing are not even in the list. I thought the script only looked at col.A... Is there a way to lock it to that?

Many thanks,
Limey (Paul)

Code: Select allfunction main() {
   //Ask the user to select the comma-delimited file
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
   //Make sure the comma-delimited file exists and then open it
   datafile = new File(csvFile);
   if (datafile.exists)
      { datafile.open('r') ;
   };
   var csvString = datafile.read();
   csvString = csvString.toLowerCase();// lets avoid Case mismatches
   datafile.close();
   // Select folder to search in
   var searchFolder = Folder.selectDialog ("Select folder to search in")
   // Select folder to save files
   var saveFolder = Folder.selectDialog ("Select folder to save files")
   var searchFiles = searchFolder.getFiles();
   for(var i=0;i<searchFiles.length;i++){
      if(csvString.indexOf(searchFiles.name.toLowerCase()) != -1){
         //there is a match between the csv and folder
         var newFilepath = saveFolder.fullName+"/"+searchFiles.name;
         if( saveFolder.exists ){
            var newFile = new File(newFilepath);
            searchFiles.copy(newFile); // copy file to new location
         }
      }
   }
};
main();



Requested
20164.tif
20250.tif
20383.tif
20655.tif
20932.tif
21132.tif
21166.tif
21306.tif
22124.tif
22189.tif
22273.tif
22538.tif
22637.tif
23109.tif
23157.tif
24414.tif
24422.tif
24438.tif
24442.tif
24886.tif
24939.tif
25947.tif
26555.tif
27351.tif
27729.tif
28285.tif
28473.tif
29037.tif
29260.tif
29417.tif
29622.tif

Files Found- Bold/Red were not requested in CSV file (Column A)
20164.tif
20250.tif
20281.tif
20383.tif
20655.tif
20932.tif
21132.tif
21166.tif
21306.tif
22124.tif
22189.tif
22273.tif
22538.tif
22637.tif
23071.tif
23072.tif
23106.tif
23109.tif
23157.tif
23188.tif
23189.tif
23213.tif
23259.tif
23265.tif
23299.tif
23303.tif
23391.tif
24414.tif
24422.tif
24438.tif
24442.tif
24886.tif
24939.tif
25947.tif
26555.tif
27351.tif
27729.tif
28285.tif
28473.tif
29037.tif
29260.tif
29417.tif
29622.tif

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Find and Save files - Exact match only - CSV

Post by Mike Hale »

I would turn the logic upside down on this. Instead of trying to match the names in the searchFolder to the names in the csv( and maybe getting false matches) I would match the names in the csv to the files in the search folder. Something like this.
Code: Select all    function main() {
       //Ask the user to select the comma-delimited file
       var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
       //Make sure the comma-delimited file exists and then open it
       datafile = new File(csvFile);
       if (datafile.exists)
          { datafile.open('r') ;
       };
       var csvString = datafile.read();
       csvString = csvString.toLowerCase();// lets avoid Case mismatches
       csvArray = csvString.match(/[^\r\n]+/g);// lets convert to array of names
       datafile.close();
       // Select folder to search in
       var searchFolder = Folder.selectDialog ("Select folder to search in")
       // Select folder to save files
       var saveFolder = Folder.selectDialog ("Select folder to save files")
       //var searchFiles = searchFolder.getFiles();
       for(var i=0;i<csvArray.length;i++){
           var matchPath = new File(searchFolder+'/'+csvArray);
          if(matchPath.exists){
             //there is a match between the csv and folder
             var newFilepath = saveFolder.fullName+"/"+csvArray;
             if( saveFolder.exists ){
                var newFile = new File(newFilepath);
                matchPath.copy(newFile); // copy file to new location
             }
          }
       }
    };
    main();
Limey

Find and Save files - Exact match only - CSV

Post by Limey »

Thanks Mike for that. It seems to work well now. The only thing now that I find is that the file names are now changing to lower case once they are copied.
before they stayed the same as the original file. I used Bridge, Batch rename to change the case after the files were copied.

Paul
Mike Hale

Find and Save files - Exact match only - CSV

Post by Mike Hale »

If case is important comment out the line

csvString = csvString.toLowerCase();// lets avoid Case mismatches

However if this is for dealing with the clipping path I think I have a better way that I will post in the other thread.
Limey

Find and Save files - Exact match only - CSV

Post by Limey »

Yes thanks Mike, I think I will change it to upper case which will work even better for me.

Thanks, Paul