你不需要幾百行批處理文件來執行替換,就像這一個一樣簡單。下面的兩行的批處理文件做同樣的:
@set @a=0 // & cscript //nologo //E:JScript "%~F0" <myfile.txt> out.txt & move /Y out.txt myfile.txt & goto :EOF
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/HEADER/g,"\x0C"));
對於正則表達式語法的進一步描述,參見this page。
編輯
爲了便於測試這個程序,你可以修改它以這樣的方式
@set @a=0 /*
cscript //nologo //E:JScript "%~F0" <myfile.txt> out.txt
move /Y out.txt myfile.txt
goto :EOF */
WScript.Stdout.Write(WScript.Stdin.ReadAll().replace(/HEADER/g,"\x0C"));
然後,打開命令提示符會話,並在命令行中執行它爲了在屏幕上看到任何錯誤信息。例如:
C:>\Users\Antonio\test type myfile.txt
This is a data line
HEADER This is the first line at top of page
This is more data line
C:>\Users\Antonio\test test.bat
C:>\Users\Antonio\test cscript //nologo //E:JScript "C:\Users\Antonio\test\test.bat" 0<myfile.txt 1>out.txt
C:>\Users\Antonio\test move /Y out.txt myfile.txt
Se han movido 1 archivos.
C:>\Users\Antonio\test goto :EOF */
C:>\Users\Antonio\test type myfile.txt
This is a data line
♀ This is the first line at top of page
This is more data line
'C' = 4bits,你用'\ x0C'試過了嗎? –
我試過了,如下圖所示,而不是運氣。當我運行它時,它會將文件中的字符串'HEADER'替換爲\ x0C – Ezy
c:\ jrepl「HEADER」\ x0C/f myfile.txt/o - – Ezy