Compare filenames in a csv to files in a directory

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

Compare filenames in a csv to files in a directory

Post by Limey »

This modification is somewhat of a fine tuning I guess... I have been using a script that Paul MR worked up for me in June of last year. It's been marvelous in speeding up the process of finding and renaming files.
bb/viewtopic.php?p=11844#11844

One thing has confused me and that is why it sometimes missed files that were correctly written into the CSV, even copied directly from the file itself and pasted into the CSV. Then the other day I figured out what was causing the problem. In the files that were missed were spaces.

Q.
The script as it stands works great but could you possibly make it include renaming files that originally have spaces in the file name? (Column A in the CSV)

I saw this in one of my other scripts and I think it has something to do with what I'm asking for. That's all well and good but, umm... Where there heck would it go! LOL
Code: Select allvar line = skuFile.readln().replace(/^\s+|\s+$/g, '');

Thanks for your help,
Paul (Limey)

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

Mike Hale

Compare filenames in a csv to files in a directory

Post by Mike Hale »

This is a long thread and I am not sure which of the script posted in it you are using. But it looks like you can't add that line without rewriting how the csv file is read.

The scripts in this thread read the whole csv file in with one read call. The line you posted would only work if the csv is read in one line at a time.

Also I'm not sure it would help even then. That line replaces leading and/or trailing spaces only from each line as it is read.

Can you post the script you are using along with a line or two from the csv that is giving you trouble?
Limey

Compare filenames in a csv to files in a directory

Post by Limey »

Hey Mike,
Well that was kinda dumb on my part... I forgot to add the script as I am using it. Looks like I was in too much of a hurry to hit send.

When I get into work I will be sure to post the code that I am actually using. I kept the post in this thread as this is where the script came out of originally. I hope other people have got the value from this thread that I have.

I've got so much to learn... I'm getting a copy of Javascript for Dummies to see if I can start learning the basics of Javascript. Hopefully then I would know that that code wouldn't work
Code: Select allThe scripts in this thread read the whole csv file in with one read call. The line you posted would only work if the csv is read in one line at a time.

Also I'm not sure it would help even then. That line replaces leading and/or trailing spaces only from each line as it is read.



Thanks for the response.
Limey (Paul)
Limey

Compare filenames in a csv to files in a directory

Post by Limey »

Mike,
Here is the code that I am currently using to Find, Rename and Move files.

Edit Post...
I forgot to add that this script also changes Uppercase to Lower Case so I have to use a rename utility to revert it back to Upper. Can we prevent the case change of the file name please?

Thanks,
Paul (Limey)


Code: Select allfunction main() {
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
   datafile = new File(csvFile);
   if (datafile.exists)
      { datafile.open('r') ;
   };
   var csvString = datafile.read();
   csvString = csvString.toLowerCase();
   datafile.close();
csvString =csvString.split('\n');
   var searchFolder = Folder.selectDialog ("Select folder to search in")
  var saveFolder = Folder.selectDialog ("Select folder to save files")
   getListOfFiles(searchFolder);
for(var l =0;l<csvString.length;l++){
   var toFind=csvString[l].split(',');
   for(var i=0;i<searchFiles.length;i++){
      var m = toFind[0].toLowerCase();
      if(searchFiles.name.toLowerCase() == m){
       var ext =searchFiles.name.substring (searchFiles.name.lastIndexOf ('.'));
         var newFilepath = saveFolder.fullName+"/"+toFind[1]+ext;       
         if( saveFolder.exists  && searchFiles instanceof File){
            var newFile = new File(newFilepath);           
            searchFiles.copy(newFile);
         }
         }
      }
   }
};
searchFiles=[];
main();

function getListOfFiles(folder) {
    var fileList = folder.getFiles();
     for (var i = 0; i < fileList.length; i++) {
        var file = fileList;
      if (file instanceof File) searchFiles.push(file);
         if (file instanceof Folder) getListOfFiles(file);
   }
}
Mike Hale

Compare filenames in a csv to files in a directory

Post by Mike Hale »

The code below should now not convert to lowercase and will replace leading and trailing spaces.

Code: Select allfunction main() {
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
   datafile = new File(csvFile);
   var csvString = [];
   if (datafile.exists){
      datafile.open('r') ;
      while(!datafile.eof){// read one line at a time until end of file
            csvString.push( datafile.readln().replace(/^\s+|\s+$/g, '') );
      }
      datafile.close();
   }
   var searchFolder = Folder.selectDialog ("Select folder to search in")
  var saveFolder = Folder.selectDialog ("Select folder to save files")
   getListOfFiles(searchFolder);
for(var l =0;l<csvString.length;l++){
   var toFind=csvString[l].split(',');
   for(var i=0;i<searchFiles.length;i++){
      var m = toFind[0].toLowerCase();
      if(searchFiles.name.toLowerCase() == m){
       var ext =searchFiles.name.substring (searchFiles.name.lastIndexOf ('.'));
         var newFilepath = saveFolder.fullName+"/"+toFind[1]+ext;       
         if( saveFolder.exists  && searchFiles instanceof File){
            var newFile = new File(newFilepath);           
            searchFiles.copy(newFile);
         }
         }
      }
   }
};
searchFiles=[];
main();

function getListOfFiles(folder) {
    var fileList = folder.getFiles();
     for (var i = 0; i < fileList.length; i++) {
        var file = fileList;
      if (file instanceof File) searchFiles.push(file);
         if (file instanceof Folder) getListOfFiles(file);
   }
}
Limey

Compare filenames in a csv to files in a directory

Post by Limey »

Mike,
I ran the updated script and it still wasn't able to gather the files with spaces in the file name.

Here are some examples of file names being missed:

File names on CD
CHERIE BED.eps
Bonita Bed - TWIN SIZE.eps
Chesapeake Bed.eps
Ardisonne Bed.eps

Here is what is in the CSV file.

Code: Select all      A                               B
CHERIE BED.eps                      M6478
Bonita Bed - TWIN SIZE.eps          M6281
Chesapeake Bed.eps                  M6482
Ardisonne Bed.eps                   M6274


This entry was found just fine.
Milwakee.eps M6541

Thanks,
Paul (Limey)
Paul MR

Compare filenames in a csv to files in a directory

Post by Paul MR »

Thanks for the example Paul, please try this now...
Code: Select allfunction main() {
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
   datafile = new File(csvFile);
   var csvString = [];
   if (datafile.exists){
      datafile.open('r') ;
      while(!datafile.eof){// read one line at a time until end of file
            csvString.push( datafile.readln().replace(/^\s+|\s+$/g, '') );
      }
      datafile.close();
   }
   var searchFolder = Folder.selectDialog ("Select folder to search in")
  var saveFolder = Folder.selectDialog ("Select folder to save files")
   getListOfFiles(searchFolder);
for(var l =0;l<csvString.length;l++){
   var toFind=csvString[l].split(',');
   for(var i=0;i<searchFiles.length;i++){
      var m = toFind[0].toLowerCase();
      if(decodeURI(searchFiles.name).toLowerCase() == decodeURI(m)){
       var ext =searchFiles.name.substring (searchFiles.name.lastIndexOf ('.'));
         var newFilepath = saveFolder.fullName+"/"+toFind[1]+ext;       
         if( saveFolder.exists  && searchFiles instanceof File){
            var newFile = new File(newFilepath);           
            searchFiles.copy(newFile);
         }
         }
      }
   }
};
searchFiles=[];
main();

function getListOfFiles(folder) {
    var fileList = folder.getFiles();
     for (var i = 0; i < fileList.length; i++) {
        var file = fileList;
      if (file instanceof File) searchFiles.push(file);
         if (file instanceof Folder) getListOfFiles(file);
   }
}
Limey

Compare filenames in a csv to files in a directory

Post by Limey »

That worked!
Thank you Paul for your help! I would like to say you are the greatest but then that would make Mike and CMYK not as great as you so I will have to say "You are amongst the greatest!".

As always your help is so very much appreciated.
Thanks also to you Mike for your support.


Regards,
Paul (Limey)
Limey

Compare filenames in a csv to files in a directory

Post by Limey »

Hi Folks,
Would you please be kind enough to tweak this script to not miss files with spaces in them.
In the example below there are spaces in the file name so as the script goes through the list it will not find, move and rename the file even though it's written exactly the same in the CSV as the file name. It's as if it's not reading the spaces in the CSV or not seeing them in the file name. Unfortunately the files are named and delivered on CDs from vendors so we have to deal with whatever they give us. This script is my most used and has been priceless over the years. It has found and renamed and tens thousands of files over the last 3 years.

Many thanks,
Limey (Paul)

CSV example:
Column A ..... WWMHS-BL angle with LED.jpg
Column B ..... T1800views2

It will however find: WWMHS-BL_angle_with_LED.jpg

CODE Currently used:
Code: Select allfunction main() {
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;");
   datafile = new File(csvFile);
   var csvString = [];
   if (datafile.exists){
      datafile.open('r') ;
      while(!datafile.eof){// read one line at a time until end of file
            csvString.push( datafile.readln().replace(/^\s+|\s+$/g, '') );
      }
      datafile.close();
   }
   var searchFolder = Folder.selectDialog ("Select folder to search in")
  var saveFolder = Folder.selectDialog ("Select folder to save files")
   getListOfFiles(searchFolder);
for(var l =0;l<csvString.length;l++){
   var toFind=csvString[l].split(',');
   for(var i=0;i<searchFiles.length;i++){
      var m = toFind[0].toLowerCase();
      if(decodeURI(searchFiles.name).toLowerCase() == decodeURI(m)){
       var ext =searchFiles.name.substring (searchFiles.name.lastIndexOf ('.'));
         var newFilepath = saveFolder.fullName+"/"+toFind[1]+ext;       
         if( saveFolder.exists  && searchFiles instanceof File){
            var newFile = new File(newFilepath);           
            searchFiles.copy(newFile);
         }
         }
      }
   }
};
searchFiles=[];
main();

function getListOfFiles(folder) {
    var fileList = folder.getFiles();
     for (var i = 0; i < fileList.length; i++) {
        var file = fileList;
      if (file instanceof File) searchFiles.push(file);
         if (file instanceof Folder) getListOfFiles(file);
   }
}


Can the same fix be applied to the following which does not rename the files but does search recursively.
Code: Select all// @include '/Applications/Adobe Photoshop CS3/Presets/Scripts/stdlib.js'
function main() {
   function matchCSV(f){
      var s = new RegExp(f.name.slice(0,-4).toLowerCase()); // Search removing the extension
      for(var i =0;i<csvData.length;i++){
         if(csvData.match(s)) return true
      };
      return false
   };
   var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;"); // User specifies the csv
   //Make sure the comma-delimited file exists and then open it with read permissions
   datafile = new File(csvFile);
   if (datafile.exists) {
      datafile.open('r') ;
   };
   var csvData = new Array();
   while(!datafile.eof){ //read each line to end of file
      csvData.push(datafile.readln().toLowerCase());// lets avoid Case mismatches
   }
   datafile.close();
   var searchFolder = Folder.selectDialog ("Select folder to search in") // Select folder to search in
   var saveFolder = Folder.selectDialog ("Select folder to save files") // Select folder to save files
   if (searchFolder != null && saveFolder != null) {
      var searchFiles = searchFolder.getFiles();
      var searchFiles = Stdlib.findFiles(searchFolder); // to search subfolders
      for(var i=0;i<searchFiles.length;i++){
         if(matchCSV(searchFiles)){ // Look for a match of the first x number of characters in the filename
            var newFilepath = saveFolder.fullName+"/"+searchFiles.name; // Create a reference to the path and the name of the file
            if( saveFolder.exists  && searchFiles instanceof File){ // Checks to see if the save folder exists and that it is not a folder but a file
               var newFile = new File(newFilepath);
               searchFiles[i].copy(newFile); // copy file to new location
            }
         }
      }
   }
};
main();
Paul MR

Compare filenames in a csv to files in a directory

Post by Paul MR »

You could try changing
Code: Select all if(matchCSV(searchFiles)){ // Look for a match of the first x number of characters in the filename
To
Code: Select all if(matchCSV(decodeURI(searchFiles))){ // Look for a match of the first x number of characters in the filename