Skip to main content
Participant
September 26, 2022
Question

Move all files in a folder that match a subfolder name into that subfolder

  • September 26, 2022
  • 2 replies
  • 408 views

Really stuck here.

I've managed to get all files exported into a folder that looks like the following:

project-name-1-portrait.jpg
project-name-1-landscape.jpg
project-name-1-square-large.jpg
project-name-1-square-small.jpg

project-name-2-portrait.jpg
project-name-2-landscape.jpg
project-name-2-square-large.jpg
project-name-2-square-small.jpg
etc...

 

In this folder also exists subfolders named:

project-name-1
project-name-2
etc...

What script would I place at the end of my script that would go through and clean up these files by moving the appropriate file into its appropriate matching folder?

Thanks in advance!

This topic has been closed for replies.

2 replies

Chuck Uebele
Community Expert
Community Expert
September 26, 2022

Here's my version of the script. You need to change the folder paths, etc.

 

var sourceF = new Folder('/c/test2')
var target1 = new Folder('/c/test2/Unit A/')
var target2 = new Folder('/c/test2/Unit B/')
var folder1Name = target1.name
var folder2Name = target2.name
var mask = '*.jpg'
var fileList = sourceF.getFiles (mask)

for(var i=0;i<fileList.length;i++){
    var curFileName = decodeURI(fileList[i].name).substr (0, 14)   
    var decodeName = decodeURI(fileList[i].name)
    switch(curFileName){
        case decodeURI(target1.name):
            fileList[i].copy(target1 + '/' +decodeName)
            fileList[i].remove()    
            break;
        case decodeURI(target2.name):
            fileList[i].copy(target2 + '/' +decodeName)
            fileList[i].remove()    
            break;            
        }
    }

 

c.pfaffenbichler
Community Expert
Community Expert
September 26, 2022

Please post meaningful screenshots. 

 

I suspect it would be more sensible to save the files to the correct Folders right away. 

If I remember correctly the JavaScript-version used for Photoshop-Scripting does not allow the moving of Files, but there is the work-around of copying the File to the target Folder and removing the original one. 

This affects the creation date of the »moved« file, though. 

Participant
September 26, 2022

I'm not sure if screenshots would help in this case. 

But you are likely correct that saving them in the right folder straight away would make sense. The trouble I have with that is I have code that is opening all files in a folder and going through them sequentially to see if they have 'portrait' or 'landscape' at the end of the file and running tasks based on that, and saving them to an export folder.

I know that I can output many of them to the correct named folder, but I also have files that do not need any actions running on them — these can be any name eg: 'project-name-2-randomName.jpg', and they are simply exported to the export folder along with the others. As they have random names after the project name, I don't know a way of saving those to the correct folder. 

 

Could you suggest a way of getting all subfolder names in a folder, saving them to an array and checking against the filename of the image I'm editing (before saving) in order to redirect them to the named export folder?

So, for instance, a folder contains subfolders named:


project-name-1
project-name-2

project-name-3

These folder names would be saved to an array [project-name-1,project-name-2,project-name-3]. When saving my files, if the current file begins with 'project-name-2' (project-name-2-portrait), if a match exists in the array, the save location would be 'project-name-2'.

Is this possible, and how would I achieve this?

c.pfaffenbichler
Community Expert
Community Expert
September 26, 2022

This would check the Documents’ names against the Folder-names in the same Folder and alert each Match. 

As you refused to post a meaningful screenshot please test whether this works out in your set-up for yourself, please. 

// 2022, use it at your own risk;
var theFolder = Folder("~/Desktop/untitled folder 3");
if (theFolder.exists == true) {
    var theFiles = retrieveFiles (theFolder, []);
    var theFolders = retrieveFolders (theFolder, []);
    for (var m = 0; m < theFolders.length; m++) {
        var thisFolder = Folder(theFolders[m]);
        for (var n = 0; n < theFiles.length; n++) {
            var thisFile = File (theFiles[n]);
            var theName = thisFile.name.match(/(.*)\.[^\.]+$/)[1];
            if (theName.match(thisFolder.name) == thisFolder.name) {
                alert (theName)
            }
        }
    };
};
////////////////////////////////////#
////// get from subfolders //////
function retrieveFolders (theFolder, theFiles) {
	if (!theFiles) {var theFiles = []};
	var theContent = theFolder.getFiles();
	for (var n = 0; n < theContent.length; n++) {
		var theObject = theContent[n];
		if (theObject.constructor.name == "Folder") {
//			theFiles = retrieveFolders(theObject, theFiles);
            theFiles.push(theObject)
			};
		};
	return theFiles
	};
////// get from subfolders //////
function retrieveFiles (theFolder, theFiles) {
	if (!theFiles) {var theFiles = []};
	var theContent = theFolder.getFiles();
	for (var n = 0; n < theContent.length; n++) {
		var theObject = theContent[n];
		if (theObject.constructor.name == "Folder") {
			theFiles = retrieveFiles(theObject, theFiles)
			};
		if (theObject.name.match(new RegExp(/\.(jpe|jpg|jpeg|gif|png|tif|tiff|bmp|psd|dng|pict|eps|raw|rw2|crw|cr2)/i)) != null) {
			theFiles = theFiles.concat(theObject)
			}
		};
	return theFiles
	};