2017-04-19 60 views
0

请耐心与我,因为我是新批次。复制文件,如果属性匹配

我试图将文件从一个位置复制到另一个只有两个属性匹配。

我试着周围和修改,但并未涉及成功:

set dSource=\\server5\Datapool 
set dTarget=C:\Users\folder1 
set fType=*.xml 
for /F "tokens=1,2 delims=:<>" %%a, in ('findstr "Name=\"Marc\"" *.xml|findstr "testcar=\"BENZ231\"" *.xml') do (
    copy /V "%%a" "%dTarget%\" 2>nul 
) 

所以我的目标是复制XML文件只有马克+ BENZ231比赛。

XML文件看起来象这样:

<testInfo testDuration="57" holidayCount="0" completedtask="12" Name="Marc" testVersion="13" testcar="BENZ231" 
<result testStepName="locating" sequenceNrResult="1" testStepResult="OK"> 
etc. 
</testInfo> 
</testresult> 

回答

0
-----------------test.bat-------------- 
@echo off&pushd \\server5\Datapool 
for /f %%a in ('dir /b ^| find ".xml"') do for /f %%A in ('type %%a ^| find /I "Marc" ^| find /I "BENZ231"') do copy %%a C:\Users\folder1 
-----------------test.bat-------------- 

现在明白了。这个对我来说工作得很好!

+0

会给你误报:“马库斯”,“马可波罗”,“卡马科”,“奔驰2315”...... – Stephan

0

findstr string1 string2搜索string1string2。你需要AND。如果两个字符串在同一行上,则很容易;只要找到找到的第一个字符串的结果,第二个字符串:

findstr "Name=\"Marc\"" test.xml|findstr "testcar=\"BENZ231\"" 

(注意:你要逃离每字面上"

要正确处理它在for,你有逃避一些特殊字符(|这里):

for /F "tokens=1,2 delims=:<>" %%a, in ('findstr findstr "Name=\"Marc\"" test.xml^|findstr "testcar=\"BENZ231\""') do (

注意:copy %%a ...' is not a good idea. You need the file name here. Put anotheraround to process each file individually (and you don't need tokens here; just set delims to "none" “delims =”`):

for %%f in (*.xml) do (
    for /F "delims=" %%a, in ('findstr "Name=\"Marc\"" "%%f"^|findstr "testcar=\"BENZ231\""') do (
    copy /V "%%f" "%dTarget%\" 2>nul 
) 
) 

如果字符串(可能)位于不同的行上,则必须使用不同的方法(解析文件两次; &&作品为 “如果第一个字符串被找到,那么”):

findstr "Name=\"Marc\"" test.xml >nul && findstr "testcar=\"BENZ231\"" test.xml 
+0

感谢您的快速反应和信息!我会在我的原始帖子中更新它。此外,我还必须修改我的副本,但我不知道如何。是的,两个字符串都在同一行上。 – Zaynqx