2017-04-19 2 views
0

私はバッチが初めてのので、私にご負担ください。アトリビュートが一致する場合はファイルをコピーしてください

2つの属性が一致する場合にのみ、ある場所から別の場所にファイルをコピーしようとしています。

私の周りしようとしたが、変更に成功しませんでした:

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 
) 

だから私の目標は、唯一のMarc + BENZ231が一致した場合にXMLファイルをコピーすることです。

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

は、 "Marcus"、 "Marco Polo"、 "Camarco"、 "Benz 2315"、... – Stephan

0

findstr string1 string2検索をstring1 OR string2ため。あなたはANDが必要です。両方の文字列が同じ行にある場合は簡単です。

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

(注意:あなたはすべての文字通り"をエスケープする必要が)ちょうど最初の文字列発見の結果で2番目の文字列を見つけ、適切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 
) 
) 

文字列が異なる行にある場合は、別の方法(ファイルを2回解析する必要があります。 "最初の文字列が、その後発見された場合" など&&作品):

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

お返事ありがとうございました!私は私のorignal post.Furtherで私も私のコピーを変更する必要がありますが、私はどのようにそれを更新します。そして、はい、両方の文字列が同じ行にあります。 – Zaynqx

関連する問題