标题: [文本处理] 【已解决】批处理删除 某一段代码 [打印本页]
作者: lovekix 时间: 2023-7-19 10:28 标题: 【已解决】批处理删除 某一段代码
本帖最后由 lovekix 于 2023-7-19 19:13 编辑
比如我有一段代码
]通过批处理 能删除吗? 每次都手动查找 然后删除太麻烦- <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">
- <uap:VisualElements DisplayName="ms-resource:WindowsBackup/Resources/WindowsBackupHostName" Description="ms-resource:WindowsBackup/Resources/WindowsBackupDescription" Square150x150Logo="WindowsBackup\Assets\MedTile.png" Square44x44Logo="WindowsBackup\Assets\AppList.png" BackgroundColor="transparent">
- ...
- </uap:VisualElements>
- <Extensions>
- <uap:Extension Category="windows.protocol">
- <uap:Protocol Name="ms-windowsbackup" ReturnResults="optional"/>
- </uap:Extension>
- <uap3:Extension Category="windows.appExtension">
- <uap3:AppExtension Name="com.microsoft.windows.extensionpackage" Id="WindowsBackup" DisplayName="Extension Metadata">
- <uap3:Properties>
- <PackageAliases>
- <PackageAlias>WindowsBackupAlias</PackageAlias>
- </PackageAliases>
- </uap3:Properties>
- </uap3:AppExtension>
- </uap3:Extension>
- <uap3:Extension Category="windows.appExtension">
- <uap3:AppExtension Name="com.microsoft.windows.app.windowsbackup" Id="WindowsBackup" DisplayName="Extension Metadata"/>
- </uap3:Extension>
- <uap3:Extension Category="windows.appExtension">
- <uap3:AppExtension Name="com.microsoft.windows.extensions.xaml" Id="WindowsBackup" DisplayName="Microsoft Windows Extensions">
- <uap3:Properties>
- <Classes>
- <WindowsBackupControl>WindowsBackup.WindowsBackupControl</WindowsBackupControl>
- </Classes>
- <MetadataProviders>
- <Provider>WindowsBackup.XamlMetaDataProvider</Provider>
- </MetadataProviders>
- <ResourceDictionaries>
- <ResourceDictionary>WindowsBackup.WindowsBackupResources</ResourceDictionary>
- </ResourceDictionaries>
- </uap3:Properties>
- </uap3:AppExtension>
- </uap3:Extension>
- </Extensions>
- <uap:ApplicationContentUriRules>
- <uap:Rule Type="include" Match="https://login.live.com/"/>
- <uap:Rule Type="include" Match="https://signup.live.com/"/>
- <uap:Rule Type="include" Match="https://account.live.com/"/>
- </uap:ApplicationContentUriRules>
- </Application>
复制代码
作者: lovekix 时间: 2023-7-19 10:37
从- <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">
复制代码
到</Application> 结束
中间有什么不重要
作者: Batcher 时间: 2023-7-19 12:00
回复 2# lovekix - sed "/<Application Id=\x22WindowsBackup\x22 Executable=\x22WindowsBackupClient.exe\x22 EntryPoint=\x22WindowsBackup.App\x22 uap10:TrustLevel=\x22mediumIL\x22 uap10:RuntimeBehavior=\x22windowsApp\x22>/,/<\/Application>/d" 1.txt > 2.txt
复制代码
命令行工具 sed.exe 下载地址:
http://bcn.bathome.net/s/tool/index.html?key=sed
作者: 77七 时间: 2023-7-19 12:18
本帖最后由 77七 于 2023-7-19 12:19 编辑
- @echo off
- rem http://bbs.bathome.net/redirect.php?goto=findpost&ptid=12042&pid=76476
- set "str1=<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
- set "str2=</Application>"
- setlocal enabledelayedexpansion
- set "str1=!str1:"="""!"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"!str1!" /c:"!str2!" "1.txt"') do if defined start (set end=%%a) else (set start=%%a)
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "1.txt"') do (
- if %%a lss !start! echo=%%b
- if %%a gtr !end! echo=%%b
- ))>"2.txt"
- endlocal
- pause
复制代码
作者: lovekix 时间: 2023-7-19 16:27
回复 3# Batcher
不会用啊
文件名是 appxmanifest.xml
作者: lovekix 时间: 2023-7-19 16:38
回复 4# 77七
不行啊 只是把 里面的内容全部复制出来了
没有 删除 那一段代码
作者: 77七 时间: 2023-7-19 16:51
回复 6# lovekix
我测试是正常的。
将代码中两处 1.txt 改为你的文件名,再试试。
如果不行 ,你把原文件传到网盘,发上来看看。
作者: lovekix 时间: 2023-7-19 17:05
回复 7# 77七
https://wwrj.lanzoue.com/iCpgY12wzlyb
作者: 77七 时间: 2023-7-19 17:27
回复 8# lovekix
你的实际文本与1楼提供的文本不符,1楼的文本行前缺少两个制表符,另外文本中存在多个 </Application>。先修改如下,有问题再说。
- @echo off
- rem http://bbs.bathome.net/redirect.php?goto=findpost&ptid=12042&pid=76476
- set "str1= <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
- set "str2= </Application>"
- setlocal enabledelayedexpansion
- set "str1=!str1:"="""!"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"!str1!" "appxmanifest.xml"') do set start=%%a
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"!str2!" "appxmanifest.xml"') do (
- if %%a gtr !start! (
- set end=%%a
- goto :next
- )
- )
- :next
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- if %%a lss !start! echo=%%b
- if %%a gtr !end! echo=%%b
- ))>"appxmanifest-2.xml"
- endlocal
- pause
复制代码
作者: lovekix 时间: 2023-7-19 17:52
回复 9# 77七
!丢了
作者: 77七 时间: 2023-7-19 17:57
回复 10# lovekix
- @echo off
- rem http://bbs.bathome.net/redirect.php?goto=findpost&ptid=12042&pid=76476
- set "str1= <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
- set "str2= </Application>"
- rem setlocal enabledelayedexpansion
- set "str1=%str1:"="""%"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str1%" "appxmanifest.xml"') do set start=%%a
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str2%" "appxmanifest.xml"') do (
- if %%a gtr %start% (
- set end=%%a
- goto :next
- )
- )
- :next
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- if %%a lss %start% echo=!str!
- if %%a gtr %end% echo=!str!
- endlocal
- ))>"appxmanifest-2.xml"
- endlocal
- pause
复制代码
作者: lovekix 时间: 2023-7-19 19:13
回复 11# 77七
谢谢
刀工完美- @echo off
- rem http://bbs.bathome.net/redirect.php?goto=findpost&ptid=12042&pid=76476
- set "str1= <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
- set "str2= </Application>"
- rem setlocal enabledelayedexpansion
- set "str1=%str1:"="""%"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str1%" "appxmanifest.xml"') do set start=%%a
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str2%" "appxmanifest.xml"') do (
- if %%a gtr %start% (
- set end=%%a
- goto :next
- )
- )
- :next
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- if %%a lss %start% echo=!str!
- if %%a gtr %end% echo=!str!
- endlocal
- ))>"appxmanifest-2.xml"
- endlocal
- pause
复制代码
作者: lovekix 时间: 2023-7-28 09:43
本帖最后由 lovekix 于 2023-7-28 10:24 编辑
回复 11# 77七
https://im.ge/i/9D1sha
无法打开 (15行的)
(for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do
加了两行命令 解决了- @echo off
- xcopy "C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\appxmanifest.xml" "%cd%\" /k /c /q /r /y
- rem http://bbs.bathome.net/redirect.php?goto=findpost&ptid=12042&pid=76476
- set "str1= <Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
- set "str2= </Application>"
- rem setlocal enabledelayedexpansion
- set "str1=%str1:"="""%"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str1%" "C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\appxmanifest.xml"') do set start=%%a
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"%str2%" "C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\appxmanifest.xml"') do (
- if %%a gtr %start% (
- set end=%%a
- goto :next
- )
- )
- :next
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- set "str=%%b"
- setlocal enabledelayedexpansion
- if %%a lss %start% echo=!str!
- if %%a gtr %end% echo=!str!
- endlocal
- ))>"C:\Windows\SystemApps\MicrosoftWindows.Client.CBS_cw5n1h2txyewy\appxmanifest.xml"
- endlocal
- del "appxmanifest.xml"
- pause
复制代码
作者: Zhanggli 时间: 2023-9-1 15:13
请教:我也是需要删除某一段代码,有点不同的是只是删除str1至str2之间代码包括str1,而str2不删除,多个文件需要处理
作者: Zhanggli 时间: 2023-9-1 15:14
T1
G00 Y48.62 F7500
G01 X9.8 F5000
G00 Y52.17 F7500
G01 X-15 F5000
G00 Y201.0 F7500
G01 X23.2 F3000
G01 X-15 F3000
G00 Y239.62 F7500
G01 X9.8 F5000
G00 Y243.17 F7500
G01 X-15 F5000
T2
G00 X-15 Y241.4 F7500
G01 X25 F5000
G01 X-15 F5000
G00 Y50.4 F7500
G01 X25.0 F5000
G01 X-15 F5000
M05
M30
作者: Zhanggli 时间: 2023-9-1 15:17
现在需要删除的是T2-M05之间的代码,包括T2行,但是不包括M05,不同文件这两者之间的代码会有不同行数,另外在M05后面加一个指令M55,多个文件需要同样处理,我总是循环不起来。
作者: hfxiang 时间: 2023-9-1 15:52
回复 15# Zhanggli
可用第3方工具sed( http://bcn.bathome.net/tool/4.9/sed.exe )- sed -e"/T2/,/M05/{/M05/!d;s/M05/M05\nM55/;}" 1.txt>2.txt
复制代码
作者: qixiaobin0715 时间: 2023-9-1 15:55
本帖最后由 qixiaobin0715 于 2023-9-1 17:21 编辑
- @echo off
- for /f "delims=" %%x in ('dir /b /a-d *.txt') do (
- set n=0
- (for /f "delims=" %%i in ('type "%%x"') do (
- if "%%i"=="T2" set n=
- if defined n echo,%%i
- if "%%i"=="M05" (
- set n=0
- echo,%%i
- echo,M55
- )
- ))>"New-%%x"
- )
- pause
复制代码
作者: Zhanggli 时间: 2023-9-1 16:51
感谢qixiaobin,真的很简洁明了,再次感谢
作者: Zhanggli 时间: 2023-9-1 16:52
我之前的思路也是和前面的定义了两个字符串变量来处理反倒没搞好
作者: Zhanggli 时间: 2023-9-1 16:58
不过如果是多个文件同样处理的话需要引入临时文件变量temp吧?
作者: 77七 时间: 2023-9-1 16:59
回复 16# Zhanggli
- @echo off
- for /f "delims=" %%i in ('dir /b /a-d *.txt') do (
- call :1 "%%i"
- )
- pause & exit
-
- :1
- setlocal
- for /f "tokens=1* delims=:" %%a in ('findstr /nxr /c:"M05" /c:"T2" "%~1"') do (
- if not defined s (
- set s=%%a
- ) else (
- set e=%%a
- )
- )
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "%~1"') do (
- if %%a lss %s% (
- echo=%%b
- ) else if %%a equ %e% (
- echo=%%b
- echo=M55
- ) else if %%a gtr %e% (
- echo=%%b
- )
- ))>$
- move /y $ "%~1"
- endlocal
- exit /b
复制代码
作者: qixiaobin0715 时间: 2023-9-1 17:21
回复 21# Zhanggli
已修改
作者: Zhanggli 时间: 2023-9-1 21:15
太赞了,我试了好几次循环都没成功,底子还是太弱了,再次感谢
作者: lovekix 时间: 2023-11-30 20:15
回复 9# 77七
微软总把 制表符 换成 空格 (有时候 9个 有时候8个)
有时候 还在会把
<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10:TrustLevel="mediumIL" uap10:RuntimeBehavior="windowsApp">"
替换成
<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10/4:TrustLevel="mediumIL" uap10/4:RuntimeBehavior="windowsApp">"
还有其他办法吗?
作者: 77七 时间: 2023-11-30 21:19
回复 25# lovekix
请把出现问题的文件传上来,看看是什么原因。
作者: lovekix 时间: 2023-12-1 08:48
本帖最后由 lovekix 于 2023-12-1 08:52 编辑
回复 26# 77七
https://wwrj.lanzouu.com/i461Y1ggo9zc
作者: 77七 时间: 2023-12-1 14:52
本帖最后由 77七 于 2023-12-1 14:54 编辑
回复 27# lovekix
- @echo off
- set "str1=<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10[/]*[4]*:TrustLevel="mediumIL" uap10[/]*[4]*:RuntimeBehavior="windowsApp">"
- set "str2=</Application>"
- set "str1=%str1:"="""%"
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"[ ]*%str1%" "appxmanifest.xml"') do set start=%%a
- for /f "tokens=1 delims=:" %%a in ('findstr /nxr /c:"[ ]*%str2%" "appxmanifest.xml"') do (
- if %%a gtr %start% (
- set end=%%a
- goto :next
- )
- )
- :next
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- if %%a lss %start% echo=%%b
- if %%a gtr %end% echo=%%b
- ))>"appxmanifest-2.xml"
- pause
复制代码
作者: lovekix 时间: 2023-12-1 17:57
回复 28# 77七
这行代码 可以用* 代替后面的吗?- set "str1=<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10[/]*[4]*:TrustLevel="mediumIL" uap10[/]*[4]*:RuntimeBehavior="windowsApp">"
复制代码
- set "str1=<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App"*>"
复制代码
作者: 77七 时间: 2023-12-1 18:24
回复 29# lovekix
.*
前面加个.
作者: terse 时间: 2023-12-1 19:24
本帖最后由 terse 于 2023-12-1 19:39 编辑
直接删除节点呢 修改把文件名传进去- @set @n=0;/* & echo off & cscript -nologo -e:jscript "%~0" "appxmanifest.xml" "appxmanifest2.xml"&pause & exit */
- var xml = new ActiveXObject("Msxml2.DOMDocument");
- xml.load(WScript.Arguments(0));
- var node = xml.selectSingleNode("//Application[@Id='WindowsBackup' and @Executable='WindowsBackupClient.exe' and @EntryPoint='WindowsBackup.App']");
- if (node != null) {
- node.parentNode.removeChild(node);
- }
- xml.save(WScript.Arguments(1));
复制代码
作者: lovekix 时间: 2023-12-2 07:57
回复 31# terse
cscript v 不是内部命令
作者: terse 时间: 2023-12-2 08:48
回复 32# lovekix
怎么后面跟个v? 将代码里cscript改为cscript.exe试,或者检测一下,- for %%i in (cscript.exe) do echo;文件路径 %%~$PATH:i
复制代码
作者: lovekix 时间: 2023-12-2 08:51
回复 33# terse
PE 下没有cscript。exe 这个文件
作者: lovekix 时间: 2023-12-2 08:55
回复 28# 77七
如果想多删除
<Application Id="WebExperienceHost" Executable="WebExperienceHostApp.exe" EntryPoint="WebExperienceHostApp.App"
这段
需要 怎么弄?
作者: terse 时间: 2023-12-2 11:59
回复 34# lovekix
PE下没测试,35楼是想是删除两节吗- @echo off
- set "str1=<Application Id="WindowsBackup" Executable="WindowsBackupClient.exe" EntryPoint="WindowsBackup.App" uap10/*[0-9]*:TrustLevel="mediumIL" uap10/*[0-9]*:RuntimeBehavior="windowsApp">"
- set "str2=<Application Id="WebExperienceHost" Executable="WebExperienceHostApp.exe" EntryPoint="WebExperienceHostApp.App""
- set "str1=%str1:"=\"%"
- set "str2=%str2:"=\"%"
- for /f "delims=:" %%a in ('findstr /ibnrc:"[ ]*%str1%" /c:"[ ]*%str2%" "appxmanifest.xml"') do set _%%a=1
- set "flag="
- (for /f "tokens=1* delims=:" %%a in ('findstr /n .* "appxmanifest.xml"') do (
- if defined flag (
- for /f %%i in ("%%b") do if "%%i" == "</Application>" set "flag="
- ) else if defined _%%a (
- set "flag=1"
- ) else echo %%b
- ))>new_appxmanifest.xml
- pause
复制代码
作者: lovekix 时间: 2023-12-2 16:03
本帖最后由 lovekix 于 2023-12-2 16:24 编辑
回复 36# terse
要是 以后删除第三段 代码 需要怎么弄?
还有 莫名多出 2个 ECHO 处于关闭状态
作者: terse 时间: 2023-12-2 19:03
回复 37# lovekix
可以将echo %%b改成echo;%%b试一下
另外,要加关键词 第4至8行这样改- set "str3=<Application Id="FESearchUI" Executable="FESearchHost.exe" EntryPoint="CortanaUI.App">"
- set "str1=%str1:"=\"%"
- set "str2=%str2:"=\"%"
- set "str3=%str3:"=\"%"
- for /f "delims=:" %%a in ('findstr /ibnrc:"[ ]*%str1%" /c:"[ ]*%str2%" /c:"[ ]*%str3%" "appxmanifest.xml"') do set _%%a=1
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |