批处理文件从多个文件夹中的多个文件中删除某些字符串 [英] Batch file to remove certain string from multiple files across multiple folders

查看:699
本文介绍了批处理文件从多个文件夹中的多个文件中删除某些字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多子不同长度的每个包含数十个的.mht 的文件的文件夹。我需要帮助创建一个批处理脚本过滤掉这个某个字符串:

 < / BODY>< / HTML>中

我可以的 HTML 以加回在年底前该文件。这是我到目前为止有:

  SETLOCAL enabledelayedexpansion用于/ R %%一中(*的.mht)做(
    FOR / F跳过= 11%% B(%% A)做(如果%% b ==< / BODY>< / HTML>中德尔%% B)
    回声的东西添加>> %%一
    回声< / BODY>< / HTML>>> %%一

有什么办法来解决我当前的脚本或者任何你知道有更简单的方式做到这一点?

注意:我尝试复制以外的所有不必要的字符串到一个临时文件,但第11行包含特殊字符,例如: | 。


解决方案

更新

我添加了一个新的脚本将取代与所需的code中的搜索项权成线。该脚本可以处理特殊字符。


限制


  1. 引领关闭支架的 ] 字符将被从行的开头修剪。不是一个问题,因为应该在HTML中没有行以该字符开头。 (如果需要,这可以是固定的)

  2. 百分号 字符不能在任何搜索词或更换长期使用。

注释


  1. 行不能含有奇数个双引号的 所以我加倍的双引号的 以确保偶数。这意味着,如果你有在任的琴弦报价,他们必须也翻倍!

  2. 要使用该脚本,只需用你想在下面的行code什么替代的搜索字词替换词

    集_ =%_: 搜索词 = 更换长期


新Script.bat

 关闭@echo
SETLOCAL ENABLEEXTENSIONS DisableDelayedExpansion::限制
:: 1.任何导致闭括号]字符将被修剪由于delims =]。在(html.txt)/ R %% f执行是否存在%%〜FF(
    FOR / F令牌= 1,* delims =]%% K的(类型%%〜FF^ |查找/ N / V')做(
        设置_ = %% L
        拨打:Expand_
    )

转到结束
:扩大_
:: NULL是一个空行或行只有一个右括号。
如果没有定义_回音。 &安培; GOTO:EOF
::确保偶数双引号的。
设置_ =%_:=,%
::注入code。
集_ = _%:其中,/身体GT; = LT; code>向注入和LT; / code>< /身体GT;%
::逃生一批特殊字符。
集_ = _%:^ = ^^%
设置_ =%_:其中= ^&下;%
集_ = _%:> = ^>%
集_ = _%:放大器; = ^&安培;%
设置_ = _%:| = ^ |%
::还原报价。
集_ = _%:=%
::显示
回声(%_%
GOTO:EOF
:结束
ENDLOCAL
暂停> NUL


原始

这应该做你想要什么。无延迟扩展需要。应该支持所有特殊字符。

限制


  1. 引领关闭支架的 ] 字符将被修剪。不是一个问题,因为应该在HTML中没有行与右括号字符开头。 (这可以在需要固定的。)

  2. 百分号 字符不能在任何搜索词或更换长期使用。

注释


  1. 行不能含有奇数个双引号的 所以我加倍的双引号的 以确保偶数。这意味着,如果你在字符串中的报价进行匹配,他们必须也增加一倍。(不适用于你的情况下)

  2. 延迟扩展不能围绕这条线可以使用 FOR / F %% S IN('回声%XLINE%^ |找到/ I< / BODY>中),做(其他感叹号会导致一个问题。

Script.bat

 关闭@echo
SETLOCAL ENABLEEXTENSIONS用于/ R %% f由于(*的.mht)就是否存在%%〜FF(
    REM局限性 - 任何领先的右方括号]字符将被修剪。
    FOR / F令牌= 1,* delims =]%% K的(类型%%〜FF^ |查找/ N / V')做(
        设置XLINE =%% L
        拨打:比赛
        回声(%% L>>中%%〜DPF \\新_ %%〜nF的%%〜xF的
    )
    REM德尔%%〜FF
    REM仁%%〜DPF \\新_ %%〜nF的%%〜xF的%%〜NXF

转到结束
:匹配
SETLOCAL ENABLEEXTENSIONS DisableDelayedExpansion
REM双倍的双引号,以确保偶数双引号的。
设置XLINE =%XLINE:=,%
FOR / F %% S IN('回声XLINE%%^ |找到/ I< /身体GT;')做(
    REM添加您的code在这里注入。复制的模板下面的呼应。
    REM注意特殊字符需要转义。
    回声注入code>>中%%〜DPF \\新_ %%〜nF的%%〜xF的

ENDLOCAL
GOTO:EOF
:结束
ENDLOCAL
暂停> NUL

这将输出新文件 new_<文件名>的.mht 如果您希望用新的文件替换旧文件,只删除 REM 命令删除命令。

I have a folder with many subfolders each containing dozens of .mht files of different lengths. I need help creating a batch script to filter out this certain string:

"</BODY></HTML>"

I can the add lines of to the file before adding it back to the end. This is what I have so far:

setlocal enabledelayedexpansion

for /r %%a in (*.mht) do (
    for /f "skip=11" %%b in (%%a) do (if %%b=="</BODY></HTML>" del %%b)
    echo "stuff to add">>%%a
    echo "</BODY></HTML>">>%%a
)

Is there any way to fix my current script or perhaps any of you know any easier way to do this?

Note: I tried copying everything except the unwanted string to a temp file but first 11 lines contain special characters ex:| , . :

解决方案

Update

I have added a new script which will replace the search term with the desired code right into the line. The script can handle special characters.


Limitations

  1. Leading Close Bracket ] characters will be trimmed from the beginning of lines. Not an issue since there should be no lines in HTML beginning with this character. (This can be fixed if needed)
  2. The percent sign % character cannot be used in either the search term or replacement term.

Notes

  1. Lines cannot contain an odd number of double quotations " so I double the double quotations "" to ensure an even number. This means that if you have quotations in either of the strings, they must as well be doubled!
  2. To use the script, just replace the search term and replacement term with what you want on the following line of code.

    set "_=%_:search term=replacement term%"

New Script.bat

@echo off
setlocal EnableExtensions DisableDelayedExpansion

:: Limitations
:: 1. Any leading close bracket ] characters will be trimmed due to delims=].

for /r %%F in (html.txt) do if exist "%%~fF" (
    for /f "tokens=1,* delims=]" %%K in ('type "%%~fF" ^| find /n /v ""') do (
        set "_=%%L"
        call :Expand_
    )
)
goto End


:Expand_
:: NULL is a blank line or line with only a close bracket ].
if not defined _ echo. & goto :eof
:: Ensure even number of double quotation marks.
set "_=%_:"=""%"
:: Inject the code.
set "_=%_:</body>=<code>To Inject</code></body>%"
:: Escape batch special characters.
set "_=%_:^=^^%"
set "_=%_:<=^<%"
set "_=%_:>=^>%"
set "_=%_:&=^&%"
set "_=%_:|=^|%"
:: Revert quotations.
set "_=%_:""="%"
:: Display
echo(%_%
goto :eof


:End
endlocal
pause >nul


Original

This should do what you want. No Delayed Expansion needed. Should support all special characters.

Limitations

  1. Leading Close Bracket ] characters will be trimmed. Not an issue since there should be no lines in HTML beginning with the close bracket character. (This can be fixed if needed.)
  2. The percent sign % character cannot be used in either the search term or replacement term.

Notes

  1. Lines cannot contain an odd number of double quotations " so I double the double quotations "" to ensure an even number. This means that if you have quotations in the string to match, they must as well be doubled. (Does not apply to your scenario)
  2. Delayed Expansion cannot be used around this line for /f %%S in ('echo "%xLine%"^| find /i "</body>"') do ( else ! exclamation marks will cause an issue.

Script.bat

@echo off
setlocal EnableExtensions

for /r %%F in (*.mht) do if exist "%%~fF" (
    rem Limitation - Any leading close bracket ] characters will be trimmed.
    for /f "tokens=1,* delims=]" %%K in ('type "%%~fF" ^| find /n /v ""') do (
        set "xLine="%%L""
        call :Match
        echo(%%L>>"%%~dpF\new_%%~nF%%~xF"
    )
    rem del "%%~fF"
    rem ren "%%~dpF\new_%%~nF%%~xF" "%%~nxF"
)
goto End


:Match
setlocal EnableExtensions DisableDelayedExpansion
rem Double the double quotations to ensure even number of double quotations.
set "xLine=%xLine:"=""%"
for /f %%S in ('echo "%xLine%"^| find /i "</body>"') do (
    rem Add your code to inject here.  Copy the template echo below.
    rem Note that special characters need to be escaped.
    echo Inject Code>>"%%~dpF\new_%%~nF%%~xF"
)
endlocal
goto :eof


:End
endlocal
pause >nul

This will output the new file to new_<filename>.mht If you want to replace the old file with the new file, just remove the rem command from before the del and ren commands.

这篇关于批处理文件从多个文件夹中的多个文件中删除某些字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆