jeudi 28 avril 2016

Batch file to print first match of findstr results only to text file

Im trying to write a batch file that reads a list from fileA.txt then checks fileC.txt if match exists, if match not exist write first matching line only from fileB.txt to fileC.txt

fileA.txt example

aaa1
aaaa
aaaa4
bbb
ccc12

fileB.txt example

aaa1 some text
aaa1 blah bla
aaa1 .r
aaaa some info
aaaa blah bla
aaaa4 some name
bbb some name to
bbb more blah blah
ccc12 another name
ccc12 blah bla

resulting fileC.txt

aaa1 some text
aaaa some info
aaaa4 some name
bbb some name to
ccc12 another name

What Im trying to do

for /F %%i in (C:\filecopy\fileA.txt) do (
If exist (findstr /B /C:%%i fileC.txt) (
echo %%i exists ) else (
findstr /B /C:%%i fileB.txt >> fileC.txt )
)

But that code isnt correct and im not sure how best to handle it

Aucun commentaire:

Enregistrer un commentaire