Skip to main content
Known Participant
January 18, 2017
Answered

To move files and folders

  • January 18, 2017
  • 4 replies
  • 13192 views

Hello,

I would like to move files and folders to different locations. I am a novice to this scripting. It would be much appreciated if someone could help me exploring how to move files to different locations.

For example

Existing location: This is where files to be picked

X:\qa\Team\Saravanan\Reports

New location: This is where files to be placed

X:\qa\Team\Saravanan\mergefiles\Temp

This topic has been closed for replies.
Correct answer willcampbell7

@willcampbell7 

 

Thanks, it’s probably possible with a variation on the method shown by @Lumigraphics here:

 

https://community.adobe.com/t5/photoshop-ecosystem-discussions/exiftool-integration-script-available/m-p/13659926

 

Where the command line program ExifTool is called and has parameters passed. I'm guessing that one would just use platform-specific calls rather than ExifTool. Not sure about passing in the variables for the file and folder though... They could probably be hard-coded though if nothing else.


Yup just had to sleep on it. Photoshop has the app method "system". Sends Terminal commands on Mac, Command Prompt on Windows. So a few tweaks. One thing different is you don't pass file objects. Instead just String objects of the full paths. And now the destination includes the file name, not just the folder. An added benefit of this difference is that the file can be renamed when moved. If not just have the same name in the "to" argument, as my example call at top shows. Also this won't deal with dotbar files on a Windows server. Those files rare nowadays anyway. That was old leftover stuff from 10 years ago, still in my first sample code I posted. Hardly anyone needs that anymore.

 

var from = "~/Desktop/in folder/green.jpg";
var to = "~/Desktop/out folder/green.jpg";
var result = moveFile(from, to);
alert(result);

function moveFile(from, to) {
    // from = String: full path and name of file to move.
    // to = String: full path and name of new file at destination.
    var command;
    var fileFrom;
    var fileTo;
    var folderTo;
    fileFrom = new File(new File(from).fsName);
    fileTo = new File(new File(to).fsName);
    if (/%(?!20|5C)/.test(File.encode(fileFrom.fsName))) {
        // Unicode characters detected in filename. Move will fail because
        // ExtendScript cannot pass Unicode text to command prompt via a string variable.
        return false;
    }
    if (!fileTo.exists) {
        folderTo = new Folder(fileTo.path);
        if (!folderTo.exists) {
            folderTo.create();
        }
        if (File.fs == "Windows") {
            command = "move \"" + fileFrom.fsName + "\" \"" + fileTo.fsName + "\"";
            app.system(command);
        } else if (File.fs == "Macintosh") {
            command = "mv \"" + fileFrom.fsName + "\" \"" + fileTo.fsName + "\"";
            app.system(command);
        }
        if (fileTo.exists) {
            return true;
        }
    }
    // Failed to move.
    return false;
}

 

 

4 replies

willcampbell7
Legend
August 26, 2020

How I move files in script.
Auto determines what OS is running and uses either VB or Applescript.
This way preserves all file attributes (date modified, etc.)
which otherwise are lost if writing new copy then deleting old.
Windows also moves any Mac dotbar files that might exist on Windows/Linux servers.

Create a File object of the file you want to move.
Create a Folder object of the destination.
Pass these two to the function.

NOTE: Windows InDesign I've gotten error about Unable to Load Adobe Type Library when calling VB on new OS installs. Run ID as Administrator, then launch script, to fix (Administrator has permission to install the needed files). Only have to run as Admin the one time.

 

function moveFile(file, folder) {
    // file = File object of the file to move.
    // folder = Folder object of destination folder.
    var AppleScript;
    var dotbar;
    var movedFile;
    var VBScript;
    if (file instanceof File && file.exists && folder instanceof Folder && folder.exists) {
        movedFile = new File(folder.fullName + "/" + file.name);
        if (!movedFile.exists) {
            if (File.fs == "Windows") {
                VBScript =
                    "Set fs = CreateObject(\"Scripting.FileSystemObject\")\r" +
                    "fs.MoveFile \"" +
                    file.fsName +
                    "\", \"" +
                    folder.fsName +
                    "\\\"";
                app.doScript(VBScript, ScriptLanguage.visualBasic);
                // Check for and move matching dotbar file if exists.
                dotbar = new File(file.path + "\\._" + file.name);
                if (dotbar.exists) {
                    VBScript =
                        "Set fs = CreateObject(\"Scripting.FileSystemObject\")\r" +
                        "fs.MoveFile \"" +
                        dotbar.fsName +
                        "\", \"" +
                        folder.fsName +
                        "\\\"";
                    app.doScript(VBScript, ScriptLanguage.VISUAL_BASIC);
                }
            } else if (File.fs == "Macintosh") {
                if (/%(?!20)/.test(File.encode(file.fsName))) {
                    // Unicode characters detected in filename. Move will fail because
                    // ExtendScript cannot pass Unicode text to AppleScript via a string variable.
                    return false;
                }
                AppleScript =
                    "tell application \"Finder\"\r" +
                    "move POSIX file \"" +
                    file.fsName +
                    "\" to POSIX file \"" +
                    folder.fsName +
                    "\" with replacing\r" +
                    "end tell\r";
                app.doScript(AppleScript, ScriptLanguage.APPLESCRIPT_LANGUAGE);
            }
            if (movedFile.exists) {
                return true;
            }
        }
    }
    // Failed to move.
    return false;
}
William Campbell
Stephen Marsh
Community Expert
Community Expert
October 31, 2023

@willcampbell7 

 

I just stumbled over your post.

 

What am I doing wrong?

 

var inFile = File("~/Desktop/in folder/green.jpg");
var outFolder = Folder("~/Desktop/out folder/");

moveFile(inFile, outFolder);

 

I'm testing on a Mac to begin with. 

willcampbell7
Legend
October 31, 2023

I would add the 'new' keyword for creating instances of objects. Also get the result, and alert its value. Should be true for success, false for failure.

 

var inFile = new File("~/Desktop/in folder/green.jpg");
var outFolder = new Folder("~/Desktop/out folder/");
var result = moveFile(inFile, outFolder);
alert(result);

Works for me. Sonoma + InDesign 2023 and 2024. What app are you trying? I've only ever used this in InDesign scripts.

 

William Campbell
Legend
March 5, 2018

I did not read the whole topic (it's hard for me)).

But it seems to me that no one uses this function if the folders are located on the same disk.

move_file("C:\\A1\\TEST.JPG", "C:\\A2\\X1\\X2\\X3")

function move_file(full_path, new_path)

    {

    var file = new File(full_path);

    create_path(new_path);

    file.rename(new_path + "\\" + file.name);

    return file.error;

    }

function create_path(path)

    {

    var folder = new Folder(path);

    if (!folder.exists)

        {

        var f = new Folder(folder.path);

        if (!f.exists) create_path(folder.path)

       

        folder.create();

        }

    return folder.error;

    }

P.S.was edited

Known Participant
March 5, 2018

Hi r-bin Once you open a file, this beautiful script creates a subfolder called "Roland Prints" then moves that original file to that subfolder "inside the main directory.

The problem is that it only moves in JPG and does not preserve the original format

Legend
March 5, 2018

I do not understand what you mean. My script does not care about the file format. And it does not open the file.

Kukurykus
Legend
January 18, 2017

If you want to move content of Reports folder (incl. all subfoldrs with their content and deeper):

(txt = File('~/Desktop/move.txt')).open('w')

txt.write('move ' + (src=(src='X:\\qa\\team\\Saravanan' )

+ '\\Reports ')  + src + '\\mergefiles\\temp')

txt.close(), txt.rename('move.bat');

(txt = File(txt.fullName.replace(/txt/, 'bat'))).execute()

bol = false; while (!bol) {

  if (bol = !Folder(SRC).exists) txt.remove(), Folder(SRC).create()

}

 

Or you may use shorter version:

system('move ' + (src=(src='X:\\qa\\team\\Saravanan' ) + '\\Reports ')  + src + '\\mergefiles\\temp'), Folder(SRC).create()

 

 

If you want to move only files of Reports folder (while subfolders stay untouched):

(txt = File('~/Desktop/move.txt')).open('w')

txt.write('move ' + (src=(src='X:\\qa\\team\\Saravanan' )

+ '\\Reports') + '\\* '  + src + '\\mergefiles\\temp')

txt.close(), txt.rename('move.bat');

(txt = File(txt.fullName.replace(/txt/, 'bat'))).execute()

bol = false; while (!bol) {

  if (bol = !File(SRC).getFiles(/\./)[0].exists) txt.remove()

}

 

Or you may use shorter version:

system('move ' + (src='X:\\qa\\team\\Saravanan' ) + '\\Reports' + '\\* '  + src + '\\mergefiles\\temp')

 

 

If you want to move content of Repports folder (without recreating it, and removing .bat file by JavaScript):

(txt = File('~/Desktop/move.txt')).open('w')

 

cde = "SET src_folder=x:\\qa\\team\\Saravanan\\Reports\n"

cde += "SET tar_folder=x:\\qa\\team\\Saravanan\\mergefiles\\temp\n"

cde += "for /f %%a IN ('dir \"%src_folder%\" /b') do move %src_folder%\\%%a %tar_folder%\n"

cde += "del %HOMEPATH%\\desktop\\move.bat"

 

txt.write(cde), txt.close(), txt.rename('move.bat');

(txt = File(txt.fullName.replace(/txt/, 'bat'))).execute(

 

 

If you wnt to move content or Repports folder (strictly by JavaScript):

for(cnt = [(dst = '/X/qa/team/Saravanan') + '/Reports'], i = 0; i < cnt.length; i++) {

  nxt = Folder(cnt).getFiles()

  for(j = 0; j < nxt.length; j++) {

    if (nxt instanceof Folder) {

       Folder((dst = dst + '/mergefiles/temp/') + nxt.fullName.match(/Reports\/(.*)/)[1]).create()

       cnt.push(nxt)

    }

    if (nxt instanceof File) File(nxt).copy(dst + nxt.name), File(nxt).remove()

  }

}

 

for(i = cnt.length - 1; i > 0; i--) File(cnt).remove()

 

 

 

Read more at How can I move all the files from one folder to another using the command line?

mauricior6328708
Inspiring
January 20, 2017

Kukurykus this is a very interesting and very useful script.

I'm looking for a script like this, in my case I need a script that just moves the file into a subfolder called "Final" within that same directory. It would be possible?

It would work as follows:

1º - I edit an image (iso made by me)

2º - Save and close this file "It can be in JPG."

3º - This file is saved and moved a subfolder called "Final" in the same directory.

Kukurykus
Legend
January 20, 2017

I don't have time right now as I'm at work, but you don't need to move saved file to other folder. You can save edited .jpg file directly to a 'Final' subfolder and then remove original file from parent of that 'Final" folder. Both things will be made by a script. So:

1) manually: you open previously saved file and edit it

2) by a script: you save a file in 'Final' subfolder, and remove original one from its parent

or if you really want:

1) manually: you open previously saved file and edit it

2) by a script: you save a file in original location, and move it to 'Final' subfolder

In my opnion the first metod in this case is more appriopate, becasue sometimes original file you open before saving may be in other format, ie .psd (when I have time i'll write it by these 2 methods)

JJMack
Community Expert
Community Expert
January 18, 2017

Please search before asking you will get answere faster that way.

https://forums.adobe.com/search.jspa?place=%2Fplaces%2F1383833&q=Move+Files&sort=updatedDesc

It is also more a thing you would do using a utility in Windows or Apple OSX perhaps with Adobe Bridge.

JJMack