Home > front end >  Setting name of current file being read as variable
Setting name of current file being read as variable

Time:04-20

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.

CodePudding user response:

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).

  • Related