保存到E:\duanwenxue\moveFiles.js- var fso = new ActiveXObject('Scripting.FileSystemObject'),
- shell = new ActiveXObject('WScript.Shell'),
- curDir,
- sSearch = '<div class="breadcrumb">当前位置:<a href=\'/\'>短篇原创文学</a>.*</div>',
- findstrStringFile = 'SearchStrings.txt',
- findstrResultFile = 'SearchResult.txt',
- logFile = 'moveHtml.log',
- ts,
- tsLog,
- nExitCode,
- sPrevFile,
- sPrevDestFolder,
- sCurrFile,
- aFilePath,
- nMultiCount,
- reFilePath = /^([^:]+):<div class="breadcrumb">当前位置:(?=<a href='\/'>短篇原创文学<\/a>>)(?:<a href='([^']+)'>[^<]+<\/a>>)+[^<]+<\/div>/i;
- curDir = shell.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName);
- try {
- tsLog = fso.OpenTextFile(logFile, 8, true, -2);
- tsLog.WriteLine(new Date().toLocaleString());
- } catch (e) {
- WScript.Quit(11);
- }
- try {
- ts = fso.OpenTextFile(findstrStringFile, 2, true, -2);
- ts.WriteLine(sSearch);
- ts.Close();
- } catch (e) {
- WScript.Quit(12);
- }
- shell.Run('%comspec% /c "pushd "' + curDir + '"&findstr.exe /irsg:"' + findstrStringFile + '" *.html >"' + findstrResultFile + '""', 0, true);
- try {
- ts = fso.OpenTextFile(findstrResultFile, 1, false, -2);
- } catch (e) {
- WScript.Quit(13);
- }
- sPrevFile = '';
- nMultiCount = NaN;
- sPrevDestFolder = curDir;
- while (!ts.AtEndOfStream) {
- aFilePath = ts.ReadLine().match(reFilePath);
- if (!aFilePath) continue;
- sCurrFile = aFilePath[1];
- if (sCurrFile === sPrevFile) {
- nMultiCount += 1;
- } else {
- if (nMultiCount === 0) {
- if (!fso.FolderExists(sPrevDestFolder)) {
- shell.Run('%comspec% /c md "' + sPrevDestFolder + '"', 0, true)
- }
- if (!fso.FileExists(sPrevDestFolder + '\\' + sPrevFile.replace(/.*\\/, ''))) {
- try {
- // fso.CopyFile(sPrevFile, sPrevDestFolder + '\\', false);
- // fso.DeleteFile(sPrevFile, true);
- fso.MoveFile(sPrevFile, sPrevDestFolder + '\\');
- } catch (e) {
- tsLog.WriteLine('Moveing ' + sPrevFile + ' failed.');
- }
- }
- } else {
- nMultiCount = 0;
- }
- sPrevFile = sCurrFile;
- sPrevDestFolder = fso.GetAbsolutePathName('.' + aFilePath[2]);
- }
- }
- if (nMultiCount === 0) {
- if (!fso.FolderExists(sPrevDestFolder)) {
- shell.Run('%comspec% /c md "' + sPrevDestFolder + '"', 0, true)
- }
- if (!fso.FileExists(sPrevDestFolder + '\\' + sPrevFile.replace(/.*\\/, ''))) {
- try {
- // fso.CopyFile(sPrevFile, sPrevDestFolder + '\\', false);
- // fso.DeleteFile(sPrevFile, true);
- fso.MoveFile(sPrevFile, sPrevDestFolder + '\\');
- } catch (e) {
- tsLog.WriteLine('Moveing ' + sPrevFile + ' failed.');
- }
- }
- }
- ts.Close();
- tsLog.Close();
- fso.DeleteFile(findstrStringFile);
- fso.DeleteFile(findstrResultFile);
- WScript.Echo('Done');
- WScript.Quit(0);
复制代码
|