【问题标题】:How to replace equal sign in batch script如何在批处理脚本中替换等号
【发布时间】:2013-07-16 13:19:25
【问题描述】:

我想用<Context>替换<Context useHttpOnly="false">

for /f "tokens=* delims=" %%A in ( %INTEXTFILE%) do (
SET string=%%A
setlocal enabledelayedexpansion
SET modified=!string:^<Context useHttpOnly^="false"^>=^<Context!
echo !modified! >> %OUTTEXTFILE%
endlocal
)

输出:没有任何变化。

由于等号和双引号发现问题。

【问题讨论】:

  • 在我看来,批处理对于这种东西来说太糟糕了。你可以使用这样的东西:seabreezecomputers.com/htmlstripper 来剥离属性。只需输入要保留的标签和属性,然后按提交即可。如果这是您必须多次执行的操作,我想这将行不通。
  • 批处理是处理字符串操作和特殊字符时的主要麻烦。可以做到,但并非没有限制。如果您想执行字符串操作,强烈建议您使用其他一些脚本语言,例如 JScript/VBScript 或 PowerShell。见stackoverflow.com/a/8591185/891976

标签: batch-file batch-processing


【解决方案1】:
@echo off
SET "string=<Context useHttpOnly="false">"
setlocal EnableDelayedExpansion
echo String: !string!
for /F "tokens=1,2 delims==>" %%i in ("!string!") do (
   if "%%i" equ "<Context useHttpOnly" if "%%~j" equ "false" set "modified=<Context>"
)
echo Modified: !modified!

【讨论】:

  • 我试过了,但它不起作用。我已经将它写在单独的 bat 文件中并运行但没有成功。实际上我必须在 xml 文件中替换这个 字符串。所以我必须在 xml 文件中找到它并替换它......
【解决方案2】:

这是一个名为 repl.bat 的辅助批处理文件 - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

这个命令可能对你有用...

type file.txt|repl "<Context.*.false.>" "<Context>" >newfile.txt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-12
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2013-07-30
    • 2011-06-15
    相关资源
    最近更新 更多