I am moving file this way:
var idOriginFolder = 'ABCDEFG12345abcdefg';
var originFolder = DriveApp.getFolderById(idOriginFolder);
var destinationFolder = DriveApp.createFolder('New Folder');
var searchString = '"'+idOriginFolder+'" in parents'
var foundFiles = DriveApp.searchFiles(searchString);
while (foundFiles.hasNext()){
var file = foundFiles.next();
destinationFolder.addFile(file);
originFolder.removeFile(file);
}
The files are moved correctly, but the modification date of every one moved file is changed to script execution date. Do you know any way to avoid this? When I move files throught of the Web Interface of Google Drive this not happen.
In my experience, the modification date of files is not changed by moving using Drive API v3. In your question, when the files were moved using DriveApp, the modification date was changed. I think that DriveApp uses Drive API v2. So I investigated this, because I was interested in this situation.
For Drive API v2
- It was found that when the files were moved using
drive.files.update
and drive.files.patch
, the modification date was changed.
For Drive API v3
- It was found that when the files were moved using
drive.files.update
, the modification date was NOT changed.
Sample script :
The sample script for using Drive API v3 is as follows.
var idOriginFolder = 'ABCDEFG12345abcdefg';
var destinationFolder = DriveApp.createFolder('New Folder').getId();
var searchString = '"'+idOriginFolder+'" in parents'
var foundFiles = DriveApp.searchFiles(searchString);
var requests = [];
while (foundFiles.hasNext()){
var file = foundFiles.next();
requests.push({
url: "https://www.googleapis.com/drive/v3/files/" + file.getId() + "?addParents=" + destinationFolder + "&removeParents=" + idOriginFolder,
method: "patch",
headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
muteHttpExceptions: true,
});
}
var res = UrlFetchApp.fetchAll(requests);
Logger.log(res)
Note :
- From these results, it is considered that moving files by Web Interface may be due to Drive API v3.
- This is a simple sample script. So if you want to move a lot of files, I recommend to use the Batching Requests.
Reference :
- Files: update for Drive API v2
- Files: patch for Drive API v2
- Files: update for Drive API v3
- Batching Requests
If this was not useful for you, I'm sorry.