Move File To Folder

Form uploads land in one big “(File responses)” folder. Exports land in My Drive root. moveFileToFolder(fileId, folderOrId) puts a Drive file in the folder you choose and takes it out of every other parent. It’s idempotent — if the file is already there, it returns { moved: false } and changes nothing, so re-running a filing job is safe.

When to use

  • Filing Google Form upload files into per-client folders
  • Moving generated PDFs / CSVs out of My Drive root
  • Cleanup jobs that sort a dump folder by a sheet column

How to use this snippet

/** * Move a Drive file into a target folder and remove it from all other parents. * Idempotent: if the file already lives only in the target, nothing changes. * * @param {string} fileId * @param {string|GoogleAppsScript.Drive.Folder} folderOrId folder or folder id * @return {{moved: boolean, file: GoogleAppsScript.Drive.File}} */ function moveFileToFolder(fileId, folderOrId) { var file = DriveApp.getFileById(fileId); var target = typeof folderOrId === 'string' ? DriveApp.getFolderById(folderOrId) : folderOrId; var targetId = target.getId(); // 1. Where does the file live right now? var parents = []; var it = file.getParents(); while (it.hasNext()) parents.push(it.next()); var inTarget = parents.some(function (p) { return p.getId() === targetId; }); // 2. Already filed (and only there): do nothing. if (inTarget && parents.length === 1) { return { moved: false, file: file }; } // 3. moveTo swaps the parent in one call (My Drive and shared drives). if (!inTarget) { file.moveTo(target); } // 4. Older files can have several parents — remove any that aren't the target. var it2 = file.getParents(); while (it2.hasNext()) { var p = it2.next(); if (p.getId() !== targetId) p.removeFile(file); } return { moved: true, file: file }; }

Example

function fileUploadForClient(uploadUrlOrId, clientName) { var root = DriveApp.getFolderById( PropertiesService.getScriptProperties().getProperty('clientsRootId')); // Pair with extractDriveFileId + getOrCreateFolderPath var fileId = extractDriveFileId(uploadUrlOrId); var folder = getOrCreateFolderPath(root, clientName + '/Uploads'); var res = moveFileToFolder(fileId, folder); console.log('%s %s', res.moved ? 'Moved' : 'Already in place:', res.file.getName()); }

Tips:

  • The script’s account needs edit access to the file and the target folder. Moving into a shared drive also needs permission to add content there.
  • Pairs with extractDriveFileId (URLs → ids) and getOrCreateFolderPath (build Client/Uploads on demand).
  • Moving doesn’t change sharing on its own, but the file can pick up access from its new folder. Check who can see the target before filing client documents there.
  • Pass a Folder object when you’re moving many files into the same place; that saves a getFolderById per file.

Tip: NitroGAS Co-Pilot can combine extractDriveFileId, getOrCreateFolderPath, and moveFileToFolder into one filing function if you describe the folder layout you want.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension