2016-07-27 2 views
0

ファイルをコピーして同時に名前を変更したいとします。私のスクリプトは、関連するすべてのファイルをディレクトリから検索し、コピーして名前を変更します。別のディレクトリにコピーしたり、名前に何かを追加したりすることはできますが、その名前のテキストの一部を他のテキストに置き換える方法が見つかりません。Googleスクリプトでファイル名のテキストを置換する

私はreplaceとreplaceTextを試していますが、いずれもファイル名を変更するのではなく、ファイルの内容を見ています。下のコードでは、FileRenameStartにファイル名を保存し、OldmonthをNewMonthに置き換えたいが、それを行う正しい方法が見つからない。誰でも手伝うことができますか?

おかげ

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); 
    } 
} 
関連する問題