Win7上運行在Windows Vista中,set /p
命令在輸出提示開始時刪除空格和製表符。您不能使用在Windows這個方法7
對於純一批解決方案,您可以使用
@echo off
setlocal enableextensions disabledelayedexpansion
rem Configure files
set "inputFile=input.txt"
set "outputFile=output.txt"
rem A temp file is needed
set "tempFile=%temp%\%~nx0.%random%%random%%random%.tmp"
rem Retrieve a line feed into a variabla
set LF=^
rem Retrieve a SUB (0x1A) character into a variable
copy nul "%tempFile%" /a >nul
for /f "usebackq" %%a in ("%tempFile%") do set "SUB=%%a"
rem For each line inside input file
(for /f "usebackq delims=" %%a in ("%inputFile%") do (
rem Retrieve the line contents
set "line=%%a"
rem Output the line, a line feed a SUB and the CRLF (from echo) to a file
setlocal enabledelayedexpansion
>"%tempFile%" (echo(!line!!LF!!sub!)
endlocal
rem From the generated file, copy everything up to the SUB character
rem The SUB and everything after it will be discarded
>nul copy "%tempFile%" /a "%tempFile%2" /b
rem Output the generated line
type "%tempFile%2"
)) > "%outputFile%"
rem Remove temporary files used
del /q "%tempFile%*"
但更高效的解決方案將使用在操作系統中可用的本地腳本引擎。
這是一種混合CMD/JScript的批處理文件(帶有.cmd
擴展名保存)被用作
cr2lf.cmd <inputfile> outputFile
該代碼將剛剛讀取標準輸入和對於每個輸入線路輸出的同一行,接着用線飼料字符
@if (@[email protected]) @then
@echo off
rem **** batch zone *********************************************************
setlocal enableextensions disabledelayedexpansion
call :runInnerScript
exit /b
:runInnerScript
cscript //E:JScript "%~f0"
goto :eof
@end
// **** Javascript zone *****************************************************
while (!WScript.StdIn.AtEndOfStream){
WScript.StdOut.Write(WScript.StdIn.ReadLine() + '\n');
};
// All done. Exit
WScript.Quit(0);
它不起作用?你得到的和你期望的有什麼區別? –
這段代碼基本上是將CRLF(DOS格式)轉換爲LF(UNIX格式)。它在32位win XP下工作完全正常,但在每行開頭的win7 64位製表符正在消失。我不希望發生這種情況。 –