Reputation: 191
I was wondering if anyone could help me perhaps write a relatively simple batch file command that I can use to base the rest of my batch file off of. I work in a support group that supports many products and there is one in specific that I am the only one that understands the XML config files. What I am trying to do is the following: Here is an excerpt from the config file:
<!-- FILEDROP SETTINGS -->
<!-- metadataType = X - XML; F - Flat file; E - embedded in filename; B - embedded PDF with bookmarks -->
<add key="metadataType" value="E" />
What I am trying to do is to create some GUI (batch file) that a user can run. Upon running the batch file, a user would be prompted to enter the name of the file to search for. In this example, the file name is importer.config. I want the batch file to search for the string
<add key="metadataType" value="E" />
I would like for it to take the value in between the quotation marks "E" in this case and output something to the DOS window to let the user know, that this component uses Metadata embedded in file name. Of course, if the value is F, this component uses metadata from a flat file....i am just trying to spell it out to the user in laments turn instead of having the user search through this large large config file because they never seem to know where to look.
Anyone that can help would be a huge huge help as this would be a basis for the rest of my code to display values to users. I have thought that using regular expressions and FINDSTR may be the best but i have tried so many things and cant get it working
something like: (?<=<add key="metadataType" value=")\w
This would look for the string i need and then take the value that follows (E in this case)...I just dont know how to write out where to store this or how to output it to something different....any help would be appreciated!
Upvotes: 1
Views: 2685
Reputation: 130809
The regex support for FINDSTR is severely limited, and what is there does not work like what you are used to in traditional implementations. Read the documentation by typing help findstr
or findstr /?
from the command window. I also recommend reading What are the undocumented features and limitations of the Windows FINDSTR command?. The description of the regex oddities are toward the bottom of the answer.
You could download and use a Windows version of something like awk, grep, sed or perl. Or you could use VBScript or JScript.
Parsing XML with native batch is a nightmare. You could try something like the following. It is not very robust, but it will work in most cases:
@echo off
setlocal enableDelayedExpansion
for /f "delims=" %%A in ('findstr /rc:"\<add key=[\"\"]metadataType[\"\"] value=[\"\"]" "fileName.txt"') do set "ln=%%A"
set ^"ln=!ln:*"metadataType" value=!"
for /f delims^=^=^" %%A in ("!ln!") do set value=%%A
echo value=!value!
Upvotes: 2