Move file to subfolder

How do I move the current file in the “C:/Folder/myFile.jpg” directory into the subfolder within the main folder like: “C:/Folder/subfolder/myFile.jpg”
here is my code

const directory = await fs.getEntryWithUrl(`file:C:/Folder/myFile.jpg`);
const file= "myFile.jpg";
const subfolder = "subfolder";
const folder = await fs.createSessionToken(directory);
const entry = await folder.getEntry(file);
await entry.moveTo(subfolder);

Would it be possible to move any file to another folder?
Thanks for the help

The example above was just a basic example.
My goal is to add a subfolder inside the root folder of the active document and move it to that subfolder.
Here, the most I could do was create the subfolder, but the file is not moved. Please could someone point me to some solution for my script. Thanks.

// get main directory
caminhof = app.activeDocument.path.replace(/\\[^\\]*$/, "") 
const directory = await fs.getEntryWithUrl(`file:${caminhof}`) 
const persistentToken = await fs.createPersistentToken(directory);
localStorage.setItem('directory', JSON.stringify(persistentToken));

token = await JSON.parse(localStorage.getItem("directory"));
const folder = await fs.getEntryForPersistentToken(token);
const entries = await folder.getEntries(); 
//Add subfolder to main directory
let subfolder = entries.filter((entry) => entry.isFolder).find((subFolder) => === "SubFolder");
if (!subfolder) {subfolder = await folder.createFolder("SubFolder");}
//active file name
const filename =;
//move active file to subfolder
await filename.moveTo(subfolder, { overwrite: true })

here’s a basic example of moving a file, you’ll need to add checks and error handling.

      let folder = await fs.getEntryWithUrl("file://C:/Folder");
      let file = await folder.getEntry("myFile.jpg");
      // Here you add your code to check if SubFolder exists and if not create it
      let subfolder = await folder.getEntry("SubFolder");
      await file.moveTo(subfolder);

note that you don’t need tokens when working with fs, it’s only needed to work with batchPlay or to store for later use.

also if you’re using getEntryWithUrl you don’t have to bother with storing tokens since no user interaction is required.

@Maher, man you were fantastic, here it worked perfectly. I will be forever grateful for your help.

1 Like