# Move file to subfolder

**URL:** <https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166>\
**Category:** Photoshop\
**Created:** [April 9, 2023, 5:57pm UTC](https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166 "2023-04-09T17:57:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MalbertCunha](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/malbertcunha/32/2760_2.png) [@MalbertCunha](https://forums.creativeclouddeveloper.com/u/MalbertCunha)\
**Post date:** [April 9, 2023, 5:57pm UTC](https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166/1 "2023-04-09T17:57:02Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![MalbertCunha](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/malbertcunha/32/2760_2.png) [@MalbertCunha](https://forums.creativeclouddeveloper.com/u/MalbertCunha)\
**Post date:** [April 10, 2023, 1:41am UTC](https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166/2 "2023-04-10T01:41:59Z")

</div>

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.

```auto
// 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.name === "SubFolder");
if (!subfolder) {subfolder = await folder.createFolder("SubFolder");}
//active file name
const filename = app.activeDocument.name;
//move active file to subfolder
await filename.moveTo(subfolder, { overwrite: true })

```

---

<div class="post-metadata">

**Author:** ![Maher](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/maher/32/1432_2.png) [@Maher](https://forums.creativeclouddeveloper.com/u/Maher)\
**Post date:** [April 10, 2023, 1:59am UTC](https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166/3 "2023-04-10T01:59:50Z")

</div>

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

```auto
      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.

---

<div class="post-metadata">

**Author:** ![MalbertCunha](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/malbertcunha/32/2760_2.png) [@MalbertCunha](https://forums.creativeclouddeveloper.com/u/MalbertCunha)\
**Post date:** [April 10, 2023, 2:30am UTC](https://forums.creativeclouddeveloper.com/t/move-file-to-subfolder/6166/4 "2023-04-10T02:30:31Z")

</div>

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