Batch Script breakline on spaces?
I'm using the following piece of code; to change something in a Cmakefile:
for /f %%j in (CMakeLists.txt) do (
if "%%j"=="Extensions_AntTweakBar" (
echo #Extensions_AntTweakBar >> temp.tmp
) else (
if "%%j"=="Extensions_Inspection" (
echo #Extensions_Inspection >> temp.tmp
) else (
if "%%j"=="Extensions_InspectionBar" (
echo #Extensions_InspectionBar >> temp.tmp
) else (
echo %%j >> temp.tmp
)
)
)
)
I开发者_如何学JAVAt seems to work, that is, it does change the selected lines, however, if theres a space on one line, it only gets that line till the space occurs. Example:
SET( PROJECT_NAME "MyProject")
Only gets the string:
SET(
How am I supposed to get the full string then?
In your code you append a space at each line.
echo %%j[space]>> temp.tmp
Here you can remove it(works only with %%j), but normally it is better to set the redirection as prefix, like
>> temp.tmp echo %%j
With a normal variable the postfix variant could fail
set var=hallo2
echo %var%>temp.tmp
Expands to echo hallo2>temp.tmp or echo hallo 2> temp.tmp, so only the stderr would redirected. The next problem are the keywords ON OFF /?, if your file contains such a word you get unexpected results, because the echo interprets these words.
[EDIT:] To solve this you can use echo(
>>temp.tmp echo(%%j
You can get empty lines (or lines with only whitespaces), you simply need to prefix them.
for /f "delims=" %%a in ('findstr /n ".*" qut.bat') do (
set var=%%a
setlocal EnableDelayedExpansion
echo(!var:*:=!
endlocal
)
The toggling of the DelayedExpansion is neccessary to preserve "!" and "^" characters
The default delimiters are space and tab. If you override the default delimiter, it should read the entire line with spaces in between
for /f "delims=@" %%j in (CMakeLists.txt) do
I set it to @, and it ignores space as delimiter and reads the entire line. You can set it a unique character that you know will not be in the text file.
精彩评论