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
  • 412 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. 

Chuck Uebele
Community Expert
Community Expert
September 26, 2022

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

c.pfaffenbichler
Community Expert
Community Expert
September 26, 2022

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 …