批处理脚本在空格上断线?

发布于 2024-10-03 18:12:53 字数 644 浏览 0 评论 0原文

我正在使用以下代码;更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深缘浅 2024-10-10 18:12:53

在您的代码中,您在每一行添加一个空格。

echo %%j[space]>> temp.tmp

在这里您可以删除它(仅适用于 %%j),但通常最好将重定向设置为前缀,就像

>> temp.tmp echo %%j

使用普通变量后缀变体可能会失败

set var=hallo2
echo %var%>temp.tmp

扩展为 echo Hallo2>temp.tmp 或 echo Hallo 2> temp.tmp,因此只有 stderr 会被重定向。
下一个问题是关键字 ON OFF /?,如果您的文件包含这样的单词,您会得到意想不到的结果,因为 echo 会解释这些单词。

,您可以使用 echo(

>>temp.tmp echo(%%j

您可以获得空行(或只有空格的行),您只需为它们添加前缀即可。

for /f "delims=" %%a in ('findstr /n ".*" qut.bat') do (
    set var=%%a
    setlocal EnableDelayedExpansion
    echo(!var:*:=!
    endlocal
)

[编辑:]要解决这个问题 DelayedExpansion 需要保留“!”和“^”字符

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

笑叹一世浮沉 2024-10-10 18:12:53

默认分隔符是空格和制表符。如果您覆盖默认分隔符,它应该读取整行,中间有空格,

for /f "delims=@" %%j in (CMakeLists.txt) do

我将其设置为@,并且它会忽略空格作为分隔符并读取整行。您可以将其设置为您知道不会出现在文本文件中的唯一字符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文