批处理脚本在空格上断线?
我正在使用以下代码;更改 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
)
)
)
)
它似乎有效,也就是说,它确实更改了选定的行,但是,如果一行上有空格,它只会获取该行,直到空格出现为止。 示例:
SET( PROJECT_NAME "MyProject")
仅获取字符串:
SET(
那么我应该如何获取完整的字符串?
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
)
)
)
)
It 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码中,您在每一行添加一个空格。
在这里您可以删除它(仅适用于 %%j),但通常最好将重定向设置为前缀,就像
使用普通变量后缀变体可能会失败
扩展为 echo Hallo2>temp.tmp 或 echo Hallo 2> temp.tmp,因此只有 stderr 会被重定向。
下一个问题是关键字 ON OFF /?,如果您的文件包含这样的单词,您会得到意想不到的结果,因为 echo 会解释这些单词。
,您可以使用 echo(
您可以获得空行(或只有空格的行),您只需为它们添加前缀即可。
[编辑:]要解决这个问题 DelayedExpansion 需要保留“!”和“^”字符
In your code you append a space at each line.
Here you can remove it(works only with %%j), but normally it is better to set the redirection as prefix, like
With a normal variable the postfix variant could fail
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(
You can get empty lines (or lines with only whitespaces), you simply need to prefix them.
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
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.