2016-07-27 82 views
0

我想複製文件並同時對其進行重命名。我的腳本搜索所有相關文件的目錄,複製並重命名它們。我可以將它們複製到不同的目錄中,或者將名稱附加到名稱上,但是我找不到如何用其他文本替換名稱中的部分文本。替換Google腳本中的文件名中的文本

我試過替換和替換文字,似乎都沒有改變文件名,而是看看文件的內容。在下面的代碼中,我想將文件名保存到FileRenameStart中,然後用NewMonth替換Oldmonth,但我無法找到正確的方法來完成它。任何人都可以幫忙?

感謝

function newMonthFunction() { 

    // what files we want 
var OldMonth = "July 2016"; 
var NewMonth = "August 2016"; 
var FindThisText = "Core " + OldMonth; 
files = DriveApp.searchFiles('title contains "' + FindThisText + '" and mimeType = "application/vnd.google-apps.spreadsheet"'); 
var files = DriveApp.searchFiles('title contains "' + FindThisText + '" and mimeType = "application/vnd.google-apps.spreadsheet"'); 

    while (files.hasNext()) { 
    var file = files.next(); 
    Logger.log(file); 
    var file = DriveApp.getFileById(file.getId()); 
    var FileRenameStart =file.getName(); 
    var FileRenameend =FileRenameStart 
    DriveApp.getFileById(file.getId()).makeCopy(FileRenameEnd) 

    } 
} 

回答

0

您已接近解決此問題。它應該適合你:

function newMonthFunction() { 
    var OldMonth = "July 2016"; 
    var NewMonth = "August 2016"; 
    var FindThisText = "Core " + OldMonth; 
    var files = DriveApp.searchFiles('title contains "' + FindThisText + '" and mimeType = "application/vnd.google-apps.spreadsheet"'); 

    while(files.hasNext()){ 
    //getting the old file 
    var file = DriveApp.getFileById(files.next().getId()); 
    //getting the old filename and replacing the old month by the new one 
    var newName = file.getName().replace(OldMonth,NewMonth); 
    //copying the old file using a new name for copy 
    var newFile = file.makeCopy(newName); 
    //logging the new filename 
    Logger.log(newFile); 
    } 
}