I would like to write a simple windows batch file that reads from standard input a text file, and writes to standard output the same content, but as one long line, sort of like what you would get if you replaced each carriage-retur开发者_JAVA技巧n-line-feed in the input by a blank.
Anyone know how to do this?
You could paste all lines together, if the total size is less than 8192 characters.
And echo the result to the new file with only one CR/LF at the end.
setlocal EnableDelayedExpansion
set "text="
for/f "delims=" %%a in (multiLine.txt) do (
set "text=!text!%%a"
)
>newFile.txt echo !text!
You could also use set /p
to output text without CR/LF, but it can't handle equal signs nor white spaces at the front.
EDIT: The first solution works, but has problems with empty lines and lines with exclamation marks.
This is a solution that eliminates both problems.
@echo off
setlocal EnableDelayedExpansion
set file=multiLine.txt
set "AllText="
for /F "tokens=2 delims=:" %%L in ('find /c /v "" %file%') do set /a "len=%%L"
<%file% (
for /L %%n in (1,1,!len!) DO (
set "line="
set /p "line="
set "AllText=!AllText!!line!"
)
)
>newFile.txt echo(!AllText!
If your file is not big, you can slurp everything into memory. Example in vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile="c:\test\file"
Set objFile=objFS.OpenTextFile(strFile)
strData = objFile.ReadAll
objFile.Close
Wscript.Echo Replace(vbCrLf,"",strData)
with powershell:
(gc c:\file) -join ''
精彩评论