【问题标题】:Possible to remove this kind of string from a file可以从文件中删除这种字符串
【发布时间】:2013-04-24 23:51:13
【问题描述】:

我在下面描述的功能是否可以使用批处理 Windows 脚本?如果是这样,我该如何编辑我的代码来执行此功能?

我正在尝试从文件中删除多行字符串。多行字符串来自另一个文件并被读入批处理变量。然后我读取目标文件并搜索这个多行字符串,如果它存在我想从目标文件中删除它。

我下面的代码无法从目标文件中删除多行字符串。它可以成功读取输入文件。 您能帮我编辑脚本以从文件中搜索和删除多行字符串吗?

@echo off &setlocal enabledelayedexpansion

Set replace=
Set target=
Set infile=usermenuTest1.4d
Set outfile=usermenuTest2.4d

Rem Read file and store all contents in string
for /f "delims=" %%i in (%infile%) do set "target=!target! %%i"
echo %target%

Rem Remove the target string from outfile
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,* delims=¶" %%A in ( 'type "%outfile%"') do SET "string=%%A"
SET "modified=!string:%target%=%replace%!"
(echo(%modified%)>> "%outfile%"

ECHO.
PAUSE
ENDLOCAL

基本上我希望我的脚本从文件中删除以下 bold(the text inside **) 文本:

// abc

// def

// hij

**Menu "User" {
   Button "" {
      Walk_Right ""
   }
}**

【问题讨论】:

  • 你需要使用批处理吗?你不能使用powershell或vbs吗?
  • @Carko Powershell 或 VBS 脚本能否在 Windows XP 及更高版本上运行?我正在分发一个将调用此脚本的安装程序,因此用户需要能够编译和运行该脚本。 Python 会是最简单的,但不是每个人都会安装 Python 解释器

标签: windows batch-file


【解决方案1】:

试试这个:

@echo off & setlocal
Set "infile=usermenuTest1.4d"
Set "outfile=usermenuTest2.4d"
for /f "tokens=1*delims=:" %%i in ('^<"%infile%" findstr /n "^"') do (
    echo(%%j|findstr /lc:"Menu \"User\"" >nul && set /a counter=6
    set /a counter-=1
    setlocal enabledelayedexpansion
    if !counter! leq 0 (echo(%%j)>>"%outfile%"
    endlocal
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2018-10-18
    • 2017-04-01
    • 2014-12-19
    相关资源
    最近更新 更多