• 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

7.5K

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
Enthusiast ,
Nov 01, 2023 Nov 01, 2023

Copy link to clipboard

Copied

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

 

 

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 ,
Nov 02, 2023 Nov 02, 2023

Copy link to clipboard

Copied

LATEST

@willcampbell7 

 

Thank you, I'll check it out and come back to you!

 

Update: That is great, thank you so much, this is a "game changer" compared to the usual way of making a new file and removing the old file. I tested on a Mac and the file date wasn't changed and the Finder Label was retained... But strangely, the Finder "Comment" added via File Info was removed when it isn't when using the mouse to move the file between folders. I'll look into the mv command as there may be additional flags/switches/arguments that can be applied.

 

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