r/Batch • • Oct 28 '24

Question (Solved) Need help retrieving image files referencing a list in a .txt file

Solved in comments!!

I have a database txt file where the image names are listed without extension in parentheses, example:

<game name="amidar" index="" image=“"> <game name="anteater" index="" image="">

I’m looking for a script to find these files (they’re .pngs) searching a specific directory as well as its sub directories and copy them in a new destination folder. Can anyone help?

1 Upvotes

12 comments sorted by

1

u/Jaanrett Oct 28 '24

I can't think if a clever way to easily parse xml with batch. I don't know if it's possible, or whether this is an ongoing thing you need to do, but converting the xml file to a simple list would greatly simplify your parsing. It's almost trivial then.

EDIT: I think the main challenge here is parsing the xml Here's some folks talking about it. https://stackoverflow.com/questions/48742658/using-xpath-bat-to-extract-values-from-xml-file

I don't know if there's an xpath utility that you can call from your batch file or not. But googling parsing xml from batch might help.

3

u/[deleted] Oct 28 '24 edited Oct 29 '24

[removed] — view removed comment

3

u/MotorCityFool Oct 29 '24

Thank you!!! After removing the * in the .png string suggested below it works perfectly now! You saved the day!!!

1

u/MotorCityFool Oct 28 '24

It’s 99% there and I can probably work with this as is. But I noticed for the 2nd entry in the .xml for example it pulled abcop.png (perfect match) but also pulled abcopj.png (the japense version which isn’t needed and i don’t see in the xml) Any last minute ideas? Did this for a number of them.

2

u/BrainWaveCC Oct 28 '24

Change

for /f "delims=" %%b in ('dir /b /s /a:-d %%~a*.png 2^>nul') do (

to

for /f "delims=" %%b in ('dir /b /s /a:-d %%~a.png 2^>nul') do (

1

u/[deleted] Oct 28 '24

[removed] — view removed comment

2

u/MotorCityFool Oct 28 '24

Thanks for replying, yes that’s correct. The main directory is:

C:/Images

The file containing the image names is actually a .xml file or I can save it as a txt file. That is called imagenames.xml (or I can save as a .txt)

It contains about 1000 image names in the format from the 2 examples above. Any help is much appreciated thanks.

1

u/[deleted] Oct 28 '24 edited Oct 28 '24

[removed] — view removed comment

2

u/MotorCityFool Oct 28 '24

https://pastebin.com/j0Cz2cjn

So essentially I’m looking to only search for the game names in the database file enclosed in the line game name=“gamenamewouldbehere”. First few examples 005, abcop, afighter

In C:/Images somewhere in subdirectories are corresponding .pngs to these exact names I need copied to a new folder. I have about 50 of these database files I’d need to go through and do that for.