'Setting name of current file being read as variable

Using a for loop in a batch file, from a folder full of xml files, I am trying to parse an xml tag but also the file name of the current file being read.

I have searched and this question seems to be the same as mine, but for linux. I am using windows

Can I get the name of the file currently being read in a for loop?

From other answers on the forum I have put together this, but unsuccessful.

for %%a in (*.xml) do (
for /f "delims=<> tokens=3" %%b in ('findstr /i /c:"<description>" *.xml') do (
echo %%~na^|%%b>>output.txt
)
)

Is it possible to get the xml tag and echo the file name of the file the tag was read from? kind of like:

for /f "delims=<> tokens=3" %%b in ('findstr /i /c:"<description>" *.xml') do (
echo %~n0^|%%b>>output.txt
)

Except that will echo the bat file name, not the xml name.

Example final output desired (xml name|xml tag)

Atari 2600|Air Raid
Atari 2600|Airlock
Atari 2600|Alien
Nintendo Game Boy|Addams Family
Nintendo Game Boy|Adventure Island
Sega 32X|Blackthorne
Sega 32X|Mortal Kombat II

Appreciate any advice as I am just teaching myself here, I don't mind just being steered in the right direction either.



Solution 1:[1]

for /f "delims=<> tokens=3" %%b in ('findstr /i /c:"<description>" "%%a"') do (

should work for you. The current filename is in %%a. Your code would attempt to find the string in all .xml files.

Perhaps findstr /m /i /c:<description>" *.xml may give you a workable base (as a command - not as a replacement findstr command for your batch).


Supplementary after comment

SET "varlist=applicationpath missing notes junk"

FOR %%a IN (%filename1%) DO (
FOR %%w IN (%varlist%) DO SET "%%w="&for /f "delims=<> tokens=3" %%b in ('findstr /i /c:"<%%w>" "%%a"') do SET "%%w=%%b"
)
:: Show resulting values from file
FOR %%w IN (%varlist%) DO SET %%w

I'm assuming that you are using %%a as you will to set up the .xml filename - I just used a fixed text file as the source for testing.

Simply establish a list of the variables you wish to extract and alter the string that findstr locates in the file. Set the variablename to the value from the located line. If not located, leave set to nothing (which is easily tested with an if defined variablename command)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1