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

To move files and folders

Community Beginner ,
Jan 18, 2017 Jan 18, 2017

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

6.8K

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

correct answers 2 Correct answers

LEGEND , Jan 18, 2017 Jan 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=(

...

Votes

Translate

Translate
Enthusiast , Nov 01, 2023 Nov 01, 2023

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 w

...

Votes

Translate

Translate
Adobe
Explorer ,
Sep 08, 2020 Sep 08, 2020

Copy link to clipboard

Copied

Thank you so much its work 🙂

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
LEGEND ,
Aug 25, 2020 Aug 25, 2020

Copy link to clipboard

Copied

Maybe by default you have set lower quality for jpgs. Change:

 

aD.saveAs(File(fnp + aD.name), new JPEGSaveOptions()),

 

to:

 

;(jpg = new JPEGSaveOptions()).quality = 12;
aD.saveAs(File(fnp + aD.name), jpg),

 

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 Beginner ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

but not work in photoshop cc 2023 , becuase photoshop cc 2023 save as not jpg formate seen

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 Beginner ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

Hi Kurkurykus! Thank you for your help to get this scripts. It's quite useful

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
LEGEND ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

@sarshan why did you marked @mauricior6328708​​ post as an correct 'answer' to your question if that was post he made for me to show me what he means and there is no any solution you needed . I mean you marked wrong answer by mistake?

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 Beginner ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

Yes, I marked it mistakenly. I am sorry

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
LEGEND ,
Jan 26, 2017 Jan 26, 2017

Copy link to clipboard

Copied

So now is correct although the most important for me is that I learnt how to do that you needed by few ways. I had to make some tests before those codes started working. All because so far I never used system() for moving files while only once JavaScript loop. This time I had to write it from beggining because I completely forgot how I did it in past

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 Beginner ,
Apr 15, 2017 Apr 15, 2017

Copy link to clipboard

Copied

Hello Kukurykus,

I am here for another help from you.

I would like to move an active document to a specific folder in photoshop.

I am sure there is a javascript for this task but I don't know how to do it.

Here is the script I have tried but it doesn't work.

app.activedocument.move("/X/qa/Team/Saravanan/mergefiles/guna/Simple/")

Cheers

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
Explorer ,
Mar 04, 2018 Mar 04, 2018

Copy link to clipboard

Copied

Thanks for this Kukurykus.  Helped me out.

I'd like to offer a VERY small correction to one of the options in the "Correct Answer" section (see the quote below).  I believe there is a missing closing parenthesis after "execute("  in line 9.

After the correction, this worked perfectly me.  Your amazing coding has taught me very much over the last few years.  Thank you!

Kukurykus  wrote

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(

)

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
LEGEND ,
Mar 04, 2018 Mar 04, 2018

Copy link to clipboard

Copied

Thank you for posting it. I didn't notice it, but now I can't correct as editing operation is blocked after somone answered to that post. That's little mistake, but for beginners that may be not possible to find it. Funny I visited this topic yestarday after long time and had to answer to someone who posted here when I was few months inactive Now I shouldn't forget it

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
Contributor ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

Hi Kukurykus. Very useful your script, I could see here that it perfectly moves the file to the referring subfolder but only in the format "JPG". Could you make a slight modification in it so it can move with the original extension? If for example in PSD and move as JPG, I can no longer do edits in the layers.

Thanks for the Script.

Script:

Folder((fnp = (fle = (aD = activeDocument).fullName).parent + '/Roland-Prints/')).create() 

aD.saveAs(File(fnp + aD.name), new JPEGSaveOptions()), aD.close(SaveOptions.DONOTSAVECHANGES), File(fle).remove() 

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
LEGEND ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

Folder((fnp = (fle = (aD = activeDocument).fullName).parent + '/Roland-Prints/')).create()

aD.saveAs(File(fnp + aD.name)), aD.close(SaveOptions.DONOTSAVECHANGES), File(fle).remove()

That modified code save files as .psd (I think any kind of them). To move file with their original extension there is needed other method of moving. I will see later maybe r-bin​ code will be somehow accurate. Now I'm too tired to do anything.

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
People's Champ ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

The function move_folder (src_path, dst_path) moves the entire contents of the src_path folder including all subfolders to the dst_path folder even if the folders are on different disks.

Works under windows. Verified!

alert(move_folder("C:\\A1", "C:\\A2\\X1\\X2"))

//alert(move_folder("C:\\A1", "D:\\A2\\X1\\X2"))

////////////////////////////////////////////////////////////////////////////////////////////

function move_folder(src_path, dst_path)

    {

    try {

        var folder = new Folder(src_path);

        var files = folder.getFiles();

        var ret = "";

        for (var i = 0; i < files.length; i++)

            {

            var ret = move_file(files.fsName, dst_path);           

            if (ret) return ret;

            }

        return ret;

        }

    catch(e) { alert(e); throw(e); }

    }

////////////////////////////////////////////////////////////////////////////////////////////

function move_file(full_path, new_path)

    {

    try {

        var file = new File(full_path);

   

        var folder = new File(new_path);

   

        var move = false;

   

        if (file.fsName.charAt(0).toLowerCase() == folder.fsName.charAt(0).toLowerCase() && file.fsName.charAt(1) == ":" && folder.fsName.charAt(1) == ":")

            move = true;

   

        if (move)

            {

            var ret = create_path(new_path);

            if (ret) return ret;

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

            return file.error;

            }

        else

            {

            if (file.length < 0)

                {

                var ret = create_path(new_path + "\\" + file.name);

                if (ret) return ret;

                ret = move_folder(full_path, new_path + "\\" + file.name);

                if (ret) return ret;

                file.remove();

                return file.error;

                }

            else

                {

                var ret = create_path(new_path);

                if (ret) return ret;

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

                file.remove();

                return file.error;

                }

            }

   

        return "The Bug!";

   

        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;

            }

        }

    catch(e) { alert(e); throw(e); }

    }

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
People's Champ ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

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

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
Contributor ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

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

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
People's Champ ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

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

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
Contributor ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

There is no need to create a fixed path:

Follow these steps:

1: Open a photo of some folder on your computer:

2: Run this script:

Folder ((fnp = (aD = activeDocument) .fullName) .parent + '/ Roland-Prints /')). Create ()

aD.saveAs (File (fnp + aD.name), new JPEGSaveOptions ()), aD.close (SaveOptions.DONOTSAVECHANGES), File (fle) .remove ()

Obs!

You will notice that inside the main directory, you created a subfolder called "Roland-Prints" you will also notice that the file has been moved to this subfolder.

The problem is that if you run this script on files that contain multiple layers type PSD it will not serve because when being moved the file is converted to JPG.

I think that's why: new JPEGSaveOptions ()),

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
People's Champ ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

It does so, as written in the script. And what do you need? I do not understand )

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
Contributor ,
Mar 05, 2018 Mar 05, 2018

Copy link to clipboard

Copied

r-bin  escreveu

It does so, as written in the script. And what do you need? I do not understand )

It moves the file and does not preserve the original format. this script is limited to JPG format only. If you run into a .PSD file it also converts to: .JPG file.

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
Enthusiast ,
Aug 26, 2020 Aug 26, 2020

Copy link to clipboard

Copied

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

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 ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

@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. 

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
Enthusiast ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

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

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 ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

@willcampbell7 

 

Thanks for the reply.

 

I don't know when/why one uses the "new" keyword and not.

 

I am testing in Photoshop, which is the forum that this topic is in.

 

I was testing using a script panel which was unexpectedly suppressing the error.

 

When running the script "properly" I was presented with an error which wasn't apparent when first posting:

 

Error 24: app.doScript is not a function.

 

This is obviously not something available in Photoshop. Is there an equivalent?

 

Curiously, this is in the Windows block, but I'm on a Mac, so I would have thought that it would have been skipped anyway.

 

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
Enthusiast ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

It's in the Mac block also. Both call doScript, and that is right, Photoshop doesn't have the method. Unfortunately there isn't a direct equivalent. I'll have to think about a way around it. Can't think of a way off the top of my head, but usually something will come to me eventually. Anyhow, it will be on my mind and I'll respond if any bright idea reveals itself.

 

Regarding when to use the new keyword, do so whenever you want a new object. A new instance of the class. For example, if calling the static method "decode" of the File class, that wouldn't be a time to use the new keyword. You're not making an object. You're just calling a static utility method of the class. On the other hand, a script can have multiple File objects -- a source file, destination, a copy, a settings file, all sorts of file objects. Each needs a unique object. That's when to use the new keyword. Makes a new instance of the class. A new object.

 

William Campbell

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 ,
Oct 31, 2023 Oct 31, 2023

Copy link to clipboard

Copied

@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...

 

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.

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