Skip to main content
CatoPbx
Inspiring
September 28, 2023
Question

A script to batch rename files, create corresponding folders, move files, create zip files?

  • September 28, 2023
  • 1 reply
  • 2360 views

I want to run script to batch rename images in a folder that I have placed png files into, then batch create sub-folders based on each individual image’s new name into a different folder. Lastly, I would like to move the png image, and also create a zip file of each individual image and save in its corresponding sub-folder.

 

Specifically, I have 2 existing folders on my Mac desktop “For Processing” and “Processing Completed”. In the “For Processing” folder, I will add any number of png images that I have created with various filenames. When it comes time that I need to process, I rename the png images to a more meaningful and consistent naming convention eg. Map-001.png, Map-002.png, etc. Then in the different folder “Processing Completed”, I create sub-folders for each image, eg sub-folder named Map-001, the next Map-002, etc. Inside each of these sub-folders I then move the png file, as well as save a corresponding zip file, ie. “Map-001.png” and “Map-001.zip” go inside the Map-001 sub-folder.

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
September 28, 2023

Where are you stuck in creating your script?

 

As you are on a Mac and this is all Finder-level stuff, you could do it with AppleScript without even worrying about having any interaction from Adobe apps.

CatoPbx
CatoPbxAuthor
Inspiring
September 29, 2023

I have never usedAppleScript - I'll need to learn it ...

Stephen Marsh
Community Expert
Community Expert
September 29, 2023
I've only done the creation of new folders - scratching my head trying to work out how to zip files, and also move the originals.
Excuse my over-commenting - I do it so I can follow stuff when I can't think anymore ...

 

main();

function main(){

    // The folder locations of 2 main folders (change as necessary)
    var processingInFolder = Folder('~/Desktop/PROCESSING-IN');
    var processingOutFolder = Folder('~/Desktop/PROCESSING-OUT');

    // If there are files in the Processing-In folder,
    // create an array of files contained in that folder
	if (processingInFolder == null) return;
	var fileList = processingInFolder.getFiles();

	// Create sub-folders under the Processing-Out folder, based on the filenames
    // in the Processing-In folder without the file extensions

    if (processingOutFolder == null) return;
    
    var theFilenamePrefix;
    var nextFolder; 

    // Iterate over each of the files
    for (var i=0; i<fileList.length; i++) { 

        // Get the filename prefix without the extension
        theFilenamePrefix = fileList[i].name.replace(/\.[^\.]+$/, '');
 
        // Create the sub-folder in the Processing-Out folder
        nextFolder = new Folder(processingOutFolder + '/' + theFilenamePrefix); 
        if(!nextFolder.exists) {nextFolder.create() }; 
    } 

}​

 


If I remember correctly, you can copy/rename in a single step, but you then need to permanently delete the originals as there is no move file system command in ES/JS.

 

EDIT: Here is a very basic example:

 

var origDoc = File("~/Desktop/For Processing/inputFile.png");
var copyRenameDoc = File("~/Desktop/Processing Completed/outputFile.png");
File(origDoc).copy(copyRenameDoc);
origDoc.remove();

 

Personally, I think this is best done using other methods (AppleScript, Unix system script, ExifTool (Perl), Python or even Powershell for Mac).