Find and delete files using CSV (w/subfolder option)

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 delete files using CSV (w/subfolder option)

Post by Limey »

Hi folks...
I have a lot of files that I am going through. I have to delete selected files from folders and I have them in a list. I have scripts I use on a daily basis that find and copy files or find, copy and rename them to another folder.

What would be handy would be a script to select folder (where files are located) then read col. A of CSV and delete the listed files from the folder. The icing on the cake would be if an optional "include sub-folders" check box could be added to the U.I

Thinking ....
I wonder if it would be safer to move rather than delete the files, then just manually delete the moved files afterwards (once the list has been verified.)
So that would possibly be a variation of the copy script that you helped me with so, so long ago. Instead it would need to move the file from one folder to another chosen by the user.
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();


If moving the file isn't possible can it be copied as my original script does but then delete the original?

Any of the above scenarios really would work. Whatever is easiest for scripting.
BTW, Sub-folder option is not a necessity, just a useful addition.

Regards,
Limey (Paul)

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

Limey

Find and delete files using CSV (w/subfolder option)

Post by Limey »

Well, I have been playing with the script and figured it out. The non-recursive version (no sub-folders)
Just added:
Code: Select allsearchFiles.remove();

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
            searchFiles.remove(); //delete original

         }
      }
   }
};
main();


To make the script include sub-paths:
You need to download and install stdlib.js into your scripts folder, then add and adjust the following path accordingly...
Code: Select all// @include '/Applications/Adobe Photoshop CS5/Presets/Scripts/stdlib.js'
If you don't already have it stdlib.js is available as part of Xtools from Xbytor at http://ps-scripts.sourceforge.net/xtools.html

Code: Select allvar searchFiles = Stdlib.findFiles(searchFolder); // to search subfolders
and

Full code:
Code: Select all// @include '/Applications/Adobe Photoshop CS5/Presets/Scripts/stdlib.js'
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
   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();
   var searchFiles = Stdlib.findFiles(searchFolder); // to search subfolders
   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
            searchFiles.remove();

         }
      }
   }
};
main();


Cheers!
Limey