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"; }
2019/10/26笔者注:使用WinRAR命令行工具不是最优的方案,还有更好更简易的方案,请访问我的另一篇文章
推荐Total Commander工具:可以实现VS/Qt工程源码的一键备份
---
参考文献
https://blog.csdn.net/pimshell/article/details/1633607
https://blog.csdn.net/pimshell/article/details/1633610