天天看點

好工具推薦系列:使用WinRAR指令行工具,一鍵備份VC++項目

2019/10/26筆者注:使用WinRAR指令行工具不是最優的方案,還有更好更簡易的方案,請通路我的另一篇文章

推薦Total Commander工具:可以實作VS/Qt工程源碼的一鍵備份

VC++項目需要備份,如果裡面有較大的目錄和檔案(如debug/Release目錄和.ncb/.pdb檔案)時,就需要先删除。但是删除臨時檔案以後又要重新編譯,比較麻煩。有沒有更便捷的方式呢?

上網查資料,檢視winrar的幫助檔案。大體的方案如下:

1、建立一個“備份檔案清單”。列入所有要備份的檔案和目錄的名稱。

2、建立一個快捷方式,指向winrar的絕對路徑,然後加上命名行參數。

3、檢視幫助檔案知道,我們還可以指定一個“忽略檔案清單”,指定哪些檔案不必備份。

方法1:CMD指令行,注意64位和32位的WinRAR安裝路徑不同

"C:\Program Files\WinRAR\Rar.exe" a -u -r test.rar @list_append.txt -x@list_ignore.txt

執行程式可以是WinRar.exe,也可以是Rar.exe

a:表示進行的是壓縮動作

-u:進行更新式的備份

-r:備份所有子目錄

test.rar:目标檔案

-x@<lf> 排除指定檔案清單中列出的檔案。如果您使用 -x@ 而沒有清單檔案名參數,它會從标準輸入裝置讀取檔案名。

此外,需要程式員手動寫以下這兩個檔案:

list_append.txt

MFCApplication1

list_ignore.txt

MFCApplication1\Debug

MFCApplication1\MFCApplication1\Debug

請注意:

(1)路徑的斜杆不能寫錯,正确是“\”,而不是"/"。因為在DOS指令中使用反斜杠“\”作為分隔符。寫錯了會執行失敗。

(2)list_append.txt的擴充名無所謂,可以是.txt,也可以是.lst,效果一樣的。

方法2:js腳本,直接滑鼠輕按兩下即可運作(推薦)

(1)BackupList.js,自動生成list_append.txt和list_ignore.txt,注意js腳本有oRet = oName.replace(/\//g, "\\");這句話,目的是把正斜杠轉換成反斜杠。因為在DOS指令中使用反斜杠“\”作為分隔符。寫錯了會執行失敗。

/* 初始化備份清單.js

說明:

1、自動收集目前目錄下的所有目錄和檔案,

将需要備份的放入“list_append.txt”,不需要備份的放入“list_ignore.txt”。

  "list_append.txt"包含要備份的檔案(目錄)清單

  "list_ignore.txt"包含要忽略的檔案(目錄)清單

2、進行比對的“正規表達式”,預設的支援VC++項目的備份。

3、對于其他項目,可根據需要自行修改“正規表達式”(reFile/rePath)。

4、也可以直接修改已生成的"list_append.txt"和"list_ignore.txt"。

5、目前的“正規表達式”訓示不備份以下目錄和檔案:

  例如所有名含有"debug/release"的目錄,

  例如所有字尾名為".rar/.zip/.ncb/.pdb/.ilk"的檔案。

更新位址:https://blog.csdn.net/libaineu2004/article/details/89811454

版本修改曆史:

1.0: - 初始版本

*/
var WshShell = new ActiveXObject("WScript.Shell");
//目前目錄
var oCurrentDirectory = WshShell.CurrentDirectory;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(oCurrentDirectory);
//準備兩個檔案
var oListAppend = fso.CreateTextFile(oCurrentDirectory + "/list_append.txt", true, true);
var oListIgnore = fso.CreateTextFile(oCurrentDirectory + "/list_ignore.txt", true, true);
//關于“忽略”的正規表達式
var reFile = /(.rar$)|(.zip$)|(.ncb$)|(.pdb$)|(.ilk$)|(.exp$)|(.obj$)|(.pch$)|(.idb$)|(.res$)|(.manifest$)|(.suo$)|(.js$)|(.user$)|(.log$)|(leak_report.txt$)/i;
var rePath = /halcon|opencv|.tlog$|ImageSource$|.vs/i;
//檢索目錄
_LookupFolder("", folder, true);
//close
oListAppend.close();
oListIgnore.close();
//ok
WshShell.Popup("Congratulations, initialization is complete!", 0, "https://blog.csdn.net/libaineu2004", 0);
/
function _LookupFolder(oRelativePath, oFolder, bAppend) {
    if (oFolder) {
        var oRet;
        //files
        var oFiles = new Enumerator(oFolder.files);
        for (; !oFiles.atEnd(); oFiles.moveNext()) {
            var oName = oRelativePath + oFiles.item().name;
            //
            if (_checkFileName(oName)) {
                oRet = oName.replace(/\//g, "\\");
                oListIgnore.WriteLine(oRet);
            } else {
                //因為父目錄已加入,是以檔案不必加入。
                if (bAppend) {
                    oRet = oName.replace(/\//g, "\\");
                    oListAppend.WriteLine(oRet);
                }
            }
        }
        //subfolders
        var oSubFolders = new Enumerator(oFolder.subFolders);
        for (; !oSubFolders.atEnd(); oSubFolders.moveNext()) {
            var oSubFolder = oSubFolders.item();
            var oName = oRelativePath + oSubFolder.name;
            //
            if (_checkPathName(oName)) {
                oRet = oName.replace(/\//g, "\\");
                oListIgnore.WriteLine(oRet);
            } else {
                //因為父目錄已加入,是以子目錄不必加入。
                if (bAppend) {
                    oRet = oName.replace(/\//g, "\\");
                    oListAppend.WriteLine(oRet);
                }
                //檢測子目錄
                _LookupFolder(oName + "/", oSubFolder, false);
            }
        }
    }
}
function _checkFileName(oName) {
    return reFile.test(oName);
}
function _checkPathName(oName) {
    return rePath.test(oName);
}      

(2)BackupRun.js,自動打包備份

/* 執行備份.js
說明: 
1、自動執行winrar.exe程式
2、"list_append.txt"包含要備份的檔案(目錄)清單
3、"list_ignore.txt"包含要忽略的檔案(目錄)清單
4、預設采用的是更新備份的方式。
5、可針對不同的實際應用自行修改指令行的開關值(oSwitch)。
更新位址:https://blog.csdn.net/libaineu2004/article/details/89811454
版本修改曆史:
1.0: - 初始版本
*/
var WshShell = new ActiveXObject("WScript.Shell");
//取得加入清單檔案的完整路徑
var oListAppendPath = _getListAppendPath();
//取得忽略清單檔案的完整路徑
var oListIgnorePath = _getListIgnorePath();
//檢測清單檔案是否已初始化
if (!_checkFileExist(oListAppendPath, oListIgnorePath)) {
    WshShell.Popup("Please execute first BackupList.js!", 0, "https://blog.csdn.net/libaineu2004", 0);
} else {
    //取得winrar.exe的完整路徑
    var oRarPath = WshShell.RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\WinRAR.exe\\");
    //取得目标檔案名
    var oRarTarget = _getTargetPath();
    //指令/開關
    var oAction = " a ";
    var oSwitch = " -u -r ";
    //組合指令行參數
    var oCommand = "\"" + oRarPath + "\"" + oAction + oSwitch + "\"" + oRarTarget + "\"" + " @" + "\"" + oListAppendPath + "\"";
    oCommand += " -x@" + "\"" + oListIgnorePath + "\"";
    //提示
    if (6 == WshShell.Popup("If you start backing up now, click 'Yes'!", 0, "https://blog.csdn.net/libaineu2004", 4)) {
        //WshShell.Popup(oCommand, 0, "cmd", 0);
        //執行
        var oExec = WshShell.Exec(oCommand);
    }
}
function _checkFileExist(oListAppendPath, oListIgnorePath) {
    try {
        //
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        fso.GetFile(oListAppendPath);
        fso.GetFile(oListIgnorePath);
    } catch (e) {
        return false;
    }
    return true;
}
function _getListAppendPath() {
    var oCurrentDirectory = WshShell.CurrentDirectory;
    return oCurrentDirectory + "\\list_append.txt";
}
function _getListIgnorePath() {
    var oCurrentDirectory = WshShell.CurrentDirectory;
    return oCurrentDirectory + "\\list_ignore.txt";
}
function _getTargetPath() {
    var oCurrentDirectory = WshShell.CurrentDirectory;
    var n = oCurrentDirectory.lastIndexOf("\\");
    return oCurrentDirectory + oCurrentDirectory.substr(n) + ".rar";
}      

---

參考文獻

https://blog.csdn.net/pimshell/article/details/1633607 https://blog.csdn.net/pimshell/article/details/1633610

繼續閱讀