I am new at this and not programmer or a coder.
I am trying to make google app script to move folder content to another folder in google drive
I came across this link
I am using below code and it runs without error, but the files are not moved.
function myFunction() {
var source_folder = DriveApp.getFolderById('1fMCHA4-b2a2BQjO0VD2dd5m4V5WvXz9D')
var dest_folder = DriveApp.getFolderById('1LbZchJflfcnOKqSPYCdGcFTI_7AAVJIi')
function moveFiles(source_folder, dest_folder){
var files = source_folder.getFiles();
while (files.hasNext()) {
var file = files.next();
dest_folder.addFile(file);
source_folder.removeFile(file);
}
}
}
Can someone advise.
CodePudding user response:
Try:
function myFunction() {
const source_folder = DriveApp.getFolderById('1fMCHA4-b2a2BQjO0VD2dd5m4V5WvXz9D')
const dest_folder = DriveApp.getFolderById('1LbZchJflfcnOKqSPYCdGcFTI_7AAVJIi')
// Call the function:
moveFiles(source_folder, dest_folder)
function moveFiles(source_folder, dest_folder) {
const files = source_folder.getFiles()
while (files.hasNext()) {
const file = files.next()
file.moveTo(dest_folder)
}
}
}
Rather than copy and delete each file, just move it!
The main problem you had was that you were not calling
your function. You had only defined it.
Learn More:
CodePudding user response:
Similar to the first answer but uses the Drive API to perform the move
function movefiles() {
const sfldr = DriveApp.getFolderById('sid');
const dfldr = DriveApp.getFolderById('did');
const files = sfldr.getFiles()
while (files.hasNext()) {
const file = files.next()
Drive.Files.update({ "parents": [{ "id": dfldr.getId() }] }, file.getId(), null, { supportsAllDrives: true });
}
}