好的,atm脚本我正在编写一个脚本,它从多个文本文件中提取值,但似乎找不到将文件合并为单个文本文件的好方法。
FOR /f "tokens=%toknum% delims=:" %%G in ('"find /v /c "" "%~dp0\!systype!Win7Updates.txt""') do set maxcnt=%%G
在文本文件中:
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
我试图弄清楚如何在文件中创建一个区域,以便将它们分开。我想尝试找到标题,转到标题,下面将会加载它。
我想要做的例子。 (也尝试在CSV文件中执行此操作)所以我希望脚本找到windows vista补丁区域,只加载该区域下面的那些。任何人都知道这是否可能?
:Windows 7 Patches
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
:Windows Vista Patches
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
KB978601,2010\MS10-019\WinSec-MS10-019-011-P57297-Windows6.1-KB978601-x86.msu,/quiet /norestart
答案 0 :(得分:1)
你的陈述“它下面的所有东西都将被加载”尚不清楚。我假设你的意思是标题以下的所有内容,直到下一个标题。我真的不知道你的意思是“加载” - 或许将这些行附加到另一个文件?对于这个解决方案,我将简单地回应这些行。
您的测试用例非常蹩脚 - 部分内容相同,因此很难判断代码是否正常工作。这是一个更有趣的测试用例,应该更容易解释。我添加了一些!
字符来说明使用FOR循环时延迟扩展的问题。
<强>的test.txt 强>
:Windows 7 patches
Windows 7 line 1!
Windows 7 line 2!
Windows 7 line 3!
:Windows Vista patches
Windows Vista line 1!
Windows Vista line 2!
Windows Vista line 3!
:Windows XP patches
Windows XP line 1!
Windows XP line 2!
Windows XP line 3!
这是一个只响应Vista补丁部分的批处理脚本。它会在循环内切换延迟扩展以保护!
。如果延迟扩张,%% A的扩张将无法正常运作。
@echo off
setlocal disableDelayedExpansion
for /f "delims=:" %%N in ('findstr /n /c:":Windows Vista patches" test.txt') do set skip=%%N
for /f "skip=%skip% delims=" %%A in (test.txt) do (
set ln=%%A
setlocal enableDelayedExpansion
if "!ln:~0,1!"==":" (endlocal & goto :break)
endlocal
echo %%A
)
:break
上述解决方案将删除任何空白行。如果需要,有一些技巧可以保留空白行。它还会删除以;
开头的任何行,因为默认的FOR / F“EOL”选项。如果需要,还有一些技巧可以解决这个问题。