Find string in file with regex in CMD - regex

Hi i have a xml file and need to find a specific string in it.
The string i search for is a value for a xml tag. Then i need to set it to a variable. How do i do it in CMD?
We can assume that file looks something like this
<rootElement>
<childElement.version>1.0.3</childElement.version>
</rootElement>
i need to extract "1.0.3" and set it to a variable.

#echo off
for /f "tokens=3 delims=<>" %%a in ('find "childElement.version" file.xml') do set "var=%%a"
echo %var%
Note: this works with your example, but surely not for every xml file. Batch is not the right tool for xml.

Related

Extract value between two strings in batch using a CSV file

I have a CSV file which contains multiple columns. One of these columns is HTML content. My first step is to search for <<< and replace it with <<< - secondly I'm searching for >>> and replace it with >>>.
My goal is to create an array in batch. For this procedure I would like to search for all elements which look like the scheme above <<<VALUE>>> and create an array.
I found the following code but it doesn't work for me...
for /F "tokens=1-2 delims=<<<>>>-" %%a in (temp.csv) do (#echo %%a %%b)
Any suggestions?
UPDATE:
I would like to use regular expressions now, but this doesn't work either...
for /f %%x in ("temp.csv") do (
echo %%x | findstr /r "^<^<^<^(\.\?\*^)^>^>^>"
)
...any help? :)
kind regards,
markus
I'm using now the simple tool batchRex.exe from administrator.de with an regular expression. I'm using the pattern <<<(.*?)>>> to get my values and save them to an .txt file. Afterwards I read line by line from this file into an array to work further on - just in case someone has the same problem ;-)
kind regards
markus

Using findstr to pass to a variable

I've got some files I'm running with a batch file that loops through everyone in a directory and dumps certain data into a sql table. I'm adding in a time stamp that I'm passing into a variable and trying to add to the sql table using sqlcmd the only problem is that to add in all relevant columns for that entry, I need to pass the names of the files that are being added to the sql table.
Okay here's the catch... the names being added to the sql table aren't the actual file names but database names that can be found in each of these xml files (close enough to xml). So I know where that is and every single one looks something like this abcdir (rest of the name) where the abcdir is a string that starts every single database.
So I thought I could use the findstr function to get the database name but I have very little experience with regex and I'd like to be able to parse out the tags and be left with just name=abcdir (rest of the name)
** * I didn't think any of my code would really be necessary since I'm just asking questions about a particular command but if thats not the case then let me know and I'll post it* **
EDIT: Okay so each file will have something like this if opened in notepad.
<Name>ABCDir Sample Name</Name>
or
<Name>ABCDir Sample Name2</Name>
and I'd like ABCDir Sample Name to be passed to a batch variable. So I thought to use findstr.
I have very little grasp of regex but I've tried using findstr >ABCDir[A-Za-z] \path\filename.ext
As I commented above, findstr (or find) will let you scrape lines containing <Name> from a text file, and for /f "delims=<>" will let you split those lines into substrings. With findstr /n, you're looking for "tokens=3 delims=<>" to get the string between <Name> and </Name>.
Try this:
#echo off
setlocal
set "file=temp.txt"
for /f "tokens=3 delims=<>" %%I in ('findstr /n /i "<Name>" "%file%"') do (
#echo %%I
)
I'm using /n with findstr to insert line numbers. The numbers aren't needed, but the switch ensures there's always a token before <Name>. Therefore, the string you want is always tokens=3 regardless of whether the line is indented or not. Otherwise, your string could be token 3 if indented, or token 2 if not. This is easier than trying to determine whether the tags are indented or not.

Writing .txt files from lines 1 to i

I am very close to my answer, but I cant seem to find it.
I am using the Findstr function in a batch file to narrow now an entire directory to just one file.
cd ...
findstr /s /m "Desktop" *class.asasm >results1.txt
findstr /m /f:results1.txt "Production" *class.asasm >results2.txt
findstr /n /f:results2.txt "Capabilities" *class.asasm >results3.txt
TASK 1: I need to figure out a way to make findstr search backwards for a fourth string from the line number the third line was found on
TASK 2: I need to write lines 1-the one we arrive at of the file in results2.txt
the insert a .txt file. Then write the rest of the original lines.
I am writing an application in VB.Net with Visual Studios and I am having a difficult time figuring out how to complete this process. I am currently having better luch with having the application run batch files that are written within the application.
The correct solution is to find a tool that does this properly. batch/CMD does not.
Here's a script that tells you the line numbers of the 3rd and 4th match. It's probably not exactly what you want, but it is a demonstration of how one can effectively work with line numbers.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET FILE=TestFile.txt
SET _LINENO=1
SET _MATCHNO=0
SET _THIRDLINENUM=
SET _FOURTHLINENUM=
FOR /F %%l IN (%FILE%) DO (
ECHO %%l | FINDSTR "Target" %_TMP% >NUL
IF NOT ERRORLEVEL 1 (
SET /A _MATCHNO=!_MATCHNO!+1
IF !_MATCHNO!==3 SET _THIRDLINENUM=!_LINENO!
IF !_MATCHNO!==4 SET _FOURTHLINENUM=!_LINENO!
)
SET /A _LINENO=!_LINENO!+1
)
#ECHO %_THIRDLINENUM% : %_FOURTHLINENUM%
Here's what's in TestFile.txt
abcdefg
bcdefgh
Target 1
cdefghi
defghij
fghijkl
Target 2
ghijklm
hijklmn
ijklmno
jklmnop
klmnopq
lmnopqr
mnopqrs
Target 3
nopqrst
Target 4
opqrstu
pqrstuv
qrstuvw
rstuvwx
stuvwxy
tuvwxyz
If you insist on using batch/CMD (and I sometimes do when nothing else is available), and you need to get the text on line #n (otherwise, head and tail would do just fine), you could produce a similar loop but replace the code from FINDSTR down to the end of the IF statement with something that compares _LINENO with some other variable, ECHO'ing the line if it is between the two values. I don't know if IF supports logical operators, so you may have to nest the IF statements, like
IF !_LINENO! GEQ %START_LINE% IF !_LINENO! LEQ %END_LINE% #ECHO %%l
assuming you need this (from your first comment):
I still have not found a way to search starting at line xx rather than 1 or to search in reverse order
you can try this (from the command line):
for /r %i in ("file pattern") do #more "%~i" +starting_line |findstr "search string"
for /r = recursively (if you mean really reverse, please explain)
"file pattern" = files to find, eg. "*class.asasm"
starting_line = search starting line, eg. 7 (more +6)
"search string" = your search pattern, eg. "Desktop"
OR search "Desktop Production Capabilities"
AND search |findstr "Desktop"|findstr "Production"|findstr "Capabilities"

RegEx in Batch File

Hey I'm trying to create a function that parses a string passed via a browser protocol. It's a "callto://" protocol and it is in this format: "callto://5551234567/" with the persons phone number inside there. I need to extract the number and pass it to another program that dials the number. The syntax for that other program is like this: "CallClerk.exe dial=5551234567=".
I'm a beginner to batch however, and can't figure out exactly what to do. Here's my current code:
#echo off
set var=%1
set number=theirphone
FindStr /R "callto://(..........)/" %var% > %number%
start C:\Program Files (x86)\CallClerk\CallClerk.exe dial=%number%=
Exit /B
Thanks for the help!
#echo off
FOR /f "tokens=2 delims=/" %%i IN ('echo %~1') DO start "" "C:\Program Files (x86)\CallClerk\CallClerk.exe" dial=%%i=
Exit /B
should work for you (untested) - assuming your input parameter is callto://5551234567/
Note the use of quoting - the .exe needs to be quoted since it contains a space in the path. The extra pair of quotes in the window-name. If you like, you could replace that pair with "Calling %%i". This parameter is optional, but inserting it ensures that START doesn't get confused between window-title, executable-name and parameter-to-executable.
This works to extract numbers from a string.
It uses two for loops, the first one gathers all the non-numeric characters and they are used as delimiters in the second for loop to gather the numerics and dial the number.
Strings of variable lengths can be handled, as long as all numbers are used in the desired telephone number.
If you want to keep the + as a valid telephone character then include it in the first for command in the delims with the numbers.
#echo off
set "var=callto://5551234567/"
for /f "delims=0123456789" %%a in ("%var%") do set "delims=%%a"
for /f "delims=%delims%" %%a in ("%var%") do (
start "" "C:\Program Files (x86)\CallClerk\CallClerk.exe" dial=%%a=
)
You should be able to use a regex along the lines of (?<=callto:\/\/)[\d]+(?=\/) to grab the number itself. This uses a positive look ahead and look behind to make sure you are matching at least one number that is preceded by the callto:// and followed by a /.
If you left it as something like callto:\/\/[\d]+\/, then it is matching the entire string and will return back with the callto text included. If you are intending to pass just the numbers along to the next part of you code, extract them using the look ahead to guarantee the before and after conditions are met.
I did a quick test using the strings you used in your example. You can see the regex in action here.

FINDSTR command in a batch file to display variable output

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!
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!