• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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!

TOPICS
Actions and scripting

Views

180

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

Yes, you have to copy first, then delete the original. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

Then it comes down to using for- and if-clauses and a matching of the Folder- and File-names to determine which Files to copy where. 

This should not be too terribly complicated, but may need some testing … 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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
	};

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 27, 2022 Sep 27, 2022

Copy link to clipboard

Copied

Hi @c.pfaffenbichler,

 

This code works perfectly to identify the matches — is there an easy way to then move said matches to their matching folder?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 27, 2022 Sep 27, 2022

Copy link to clipboard

Copied

LATEST

If you are on Windows take @jazz-y ’s advice otherwise @Chuck Uebele ’s code contains the copy/remove-parts. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

 

fs = File("/d/1.txt")
fd = File("/d/test folder/1.txt")
alert (fs.rename(fd))

 

We can move files by .rename(). But it works only in Windows

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 26, 2022 Sep 26, 2022

Copy link to clipboard

Copied

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;            
        }
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines