Move files with JavaScript

I can't for the life of me work out how to do this.

The script is given to a button. When the button is clicked it should:

  1. Create a folder in the current dir called SS, and a subfolder of the current date, in the format YY MM DD.
  2. It should then move all the selected files to the newly created dir.

The folder is created, but no files are copied. When the folder is created, focus is shifted to the new folder so maybe that has something to do with the problem?

This is my code:

function OnClick(clickData) {
    var tab = clickData.func.sourcetab;
    var cmd = clickData.func.command;

    var currentTime = new Date();
    var year = currentTime.getFullYear() % 100;
    var month = (currentTime.getMonth() + 1).toString();
    var day = currentTime.getDate().toString();

    // Ensure month and day are two digits
    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    var pathToCreate = tab.path + "\\SS\\" + year + " " + month + " " + day;

    // Create the folder
    cmd.RunCommand('CreateFolder NAME="' + pathToCreate + '" READAUTO');
    
    // Move selected files to the newly created folder
    if (tab.selected_files.count > 0) {
        // Format the path for Windows
        cmd.RunCommand('Copy MOVE TO="' + pathToCreate)
    }
}

Could it be because you're missing a " at the end of your command?

Unfortunately not. I then get this error:

15/06/2024 11:43 Unterminated string constant (0x800a03f7)
15/06/2024 11:43 Parse error - script aborted
15/06/2024 11:43 Error at line 23, position 60
15/06/2024 11:43 cmd.RunCommand('Copy MOVE TO="' + pathToCreate + ")

It's also the same without the +.

What I meant was

cmd.RunCommand('Copy MOVE TO="' + pathToCreate + '"');

Ah right. I no longer get an error, but the files still aren't being copied. The folder is created, focus is shifted the folder, but no files are copied.

Try this:

1 Like

That works!

Thank you so much :blush:

:+1: :smiley:

BTW: Did you look at Copy CREATEFOLDER?

I did briefly although couldn't quite work it out. I assume it would be able to create the folder and move the files in one line of code instead of two.

Just tried it; I always love simplifying things...
Copy MOVE HERE CREATEFOLDER "foldername" seems to work.

Have a good weekend!

There is also a button in the default setup you could have modified:

image

I did consider this, however I wanted to create a custom button to fit in with my folder structure.