本帖最后由 WHY 于 2018-7-14 20:04 编辑
其实脚本有很大的优化空间,举例:- @echo off
- COLOR 1F
-
- set "F1=%~nx1"
- set /p F1=请输入源目录:
- set "F2=Author"
- if not exist "%F2%" md "%F2%"
-
- setlocal enabledelayedexpansion
- for /f "delims=" %%a in ('dir /b /a-d "%F1%\*.txt"') do (
- for /f "delims=" %%i in ('type "%F1%\%%a"') do (
- set "m=%%i"
- if "!m:~0,5!" == "Level" (
- set /a x=10000 + !m:~5!
- echo;>"%F2%\%%~na_!x:~1!.txt"
- ) else (
- echo;%%i>>"%F2%\%%~na_!x:~1!.txt"
- if "!m:~0,5!" == "Title" (
- for /f "delims=" %%j in ("!m:~7!") do set "Title=%%j"
- ) else if "!m:~0,6!" == "Author" (
- set "Author=!m:~8!"
- )
- )
-
- if defined Title if defined Author (
- rem 替换字符
- set "NewName=!Author!_%%~na_!x:~1!_!Title!"
- set "NewName=!NewName: + =,!"
- set "NewName=!NewName: +=,!"
- set "NewName=!NewName:+ =,!"
- set "NewName=!NewName:+=,!"
- set "NewName=!NewName: =-!"
- set "NewName=!NewName: =-!"
- set "NewName=!NewName:--=-!"
-
- rem 重命名
- ren "%F2%\%%~na_!x:~1!.txt" "!NewName!.txt"
- set "Title="
- set "Author="
- )
- )
- )
- pause & exit
复制代码 test.js- var srcDir = '.'; //源目录
- var dstDir = 'Author'; //目标目录
-
- var fso = new ActiveXObject('Scripting.FileSystemObject');
- if(!fso.FolderExists(dstDir)) fso.CreateFolder(dstDir); //创建文件夹
-
- var objFolder = fso.GetFolder(srcDir);
- var e = new Enumerator(objFolder.Files);
-
- for(; !e.atEnd(); e.moveNext()){
- var f = e.item();
- if(!/\.txt$/i.test(f.Name)) continue; //忽略非txt后缀文件
- var baseName = f.Name.replace(/\.txt$/i,'');
-
- var txt = fso.OpenTextFile(f, 1).ReadAll(); //打开、读取文本
- var arr = txt.split(/[\r\n]+Level(?=\d+)/); //分割数组
-
- for(var i=0; i<arr.length; i++) writeToFile(arr[i], baseName);
- }
-
- function writeToFile(str, baseName){
- var num = str.match(/^\d+/); //第一行数字赋值给num
- var author = str.match(/^Author:[^\r\n]+/m);
- var title = str.match(/^Title:[^\r\n]+/m);
-
- if(num && author && title){
- num = (10000 + num * 1 + '').substr(1);
- var newName = author + '_' + baseName + '_' + num + '_' + title;
-
- //删除非法文件名字符,删除 Author: 和 Title:字符,替换字符
- newName = newName.replace(/(Author|Title):\s*|[\\/|<>?*":]/g, '').replace(/\s*\+\s*/g, ',').replace(/\s+/g, '-');
-
- str = str.replace(/^[^\r\n]+/, ''); //删除第一行(数字行)
- fso.CreateTextFile(dstDir + '\\' + newName + '.txt', true).writeLine(str); //写入txt文件
- }
- }
-
- WSH.Echo('Done');
复制代码
|