Batch: Find substring with quotes in string - batch-file

I cannot get this to work. I am reading an XML file line by line and then look at each line if it contains a specific tag <assemblyIdentity name="PostDeploymentAction" version". When I find it, I would modify it and write everything back into a file.
However, I can not find the tag since it contains quote marks.
#ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
....some more code....
SET dllFile=%DestPath%\%ProjectName%.dll.manifest
IF NOT EXIST "%dllFile%" (
ECHO File %ProjectName%.dll.manifest does not exist^^!
GOTO ERROR
) ELSE (
ECHO Modifying %ProjectName%.dll.manifest in directory:
ECHO %DestPath%
REM Create a temporary file in the folder, where this batch file is being executed from
>"temp.xml" (
FOR /F "usebackq delims=" %%I IN ("%dllFile%") DO (
SET "line=%%I"
REM Insert existing line before modification
SETLOCAL DisableDelayedExpansion
ECHO %%I
ENDLOCAL
REM Find correct version number
SET "myVariable=<assemblyIdentity name="PostDeploymentAction" version"
IF not "!line!"=="!line:myVariable=!" (
echo !line!
)
....some more code....
)
)
)
Whatever escape characters I use, it will not find this particular line (or it finds every line). Everything else in above code works fine - only IF not "!line!"=="!line:myVariable=!" does not. Any help much appreciated.
Thanks

Use another method to compare the strings:
#echo off
setlocal enabledelayedexpansion
; set "myvariable=<assemblyIdentity name="PostDeploymentAction" version"
set myvariable
for /f "delims=" %%a in (test.txt) do (
echo "%%a"|findstr /c:"!myvariable:"=\"!">nul && (
echo DEBUG: found %%a
REM do something special here
) || (
echo %%a
REM write line unchanged
)
)
(you have to escape the doublequotes with findstr; findstr` uses the backslash as escape character)

In the end, I ended up with
SET "xmlTag=<assemblyIdentity name="PostDeploymentAction"
FOR /F "usebackq delims=" %%A IN ("%dllFile%") DO (
ECHO "%%A"|FINDSTR /c:"!xmlTag:"=\"!">nul && (
FOR /F "tokens=2,3 delims=^=" %%B IN ("%%A") DO (
SET version=%%C
SET version=!version:~0,-9!
ECHO Found PostDeploymentAction.dll version number !version!
)
)
)
This gives me the version number I was after. The full XML line is <assemblyIdentity name="PostDeploymentAction" version="1.1.0.0" language="neutral" processorArchitecture="msil" />. The first FOR loop gets the entire line out of the XML file, while the second FOR gets the actual version number "1.1.0.0".
The second FOR is a bit weird, but it works. However, I am sure there are better ways...

BTW, the above was a bit slow since FINDSTR is called for each line of the file (read file line by line and then analyze each line with FINDSTR).
There is a much, much faster version by calling FINDSTR only once for the entire file and then analyzing the return result found by FINDSTR.
SET "xmlTag=<assemblyIdentity name="PostDeploymentAction"
FOR /F "tokens=*" %%A in ('FINDSTR /c:^"!xmlTag:"=\"!^" ^"%dllFile%^"') DO (
FOR /F "tokens=5 delims=^=^ " %%B IN ("%%A") DO (
SET version=%%B
ECHO Found PostDeploymentAction.dll version number !version:~1,-1!
)
)

Related

How to get records right arranged in windows batch?

I have file s_result.txt as:
AAA,BBB,CCC
DDD,EEE
FFF,GGG
HHH,III,JJJ
...
And I try to get the sf_result.txt like this:
AAA,BBB
AAA,CCC
DDD,EEE
FFF,GGG
HHH,III
HHH,JJJ
...
I used script as below:
REM Transfer s_result.txt to sf_result.txt
DEL sf_result.txt
Echo. 2>sf_result.txt
for /F "tokens=1,2,3 delims=," %%a in (s_result.txt) do (
If %%c EQU [] (
ECHO %%a,%%b>>sf_result.txt
) else (
ECHO %%a,%%b>>sf_result.txt
ECHO %%a,%%c>>sf_result.txt
)
)
I got this result.txt instead:
AAA,BBB
AAA,CCC
DDD,EEE
DDD,
FFF,GGG
FFF,
HHH,III
HHH,JJJ
...
How can I get the right result?
Thanks,
If you want to parse line-by-line, use for /F. If you want to tokenize word-by-word on a single line, use for without the /F. Also, in a basic for loop, Windows already tokenizes on unquoted commas with no need to specify a delimiter. (It also tokenizes on spaces, tabs, and semicolons.) With this in mind, the solution is actually pretty simple.
#echo off & setlocal
for /f "usebackq tokens=1* delims=," %%I in ("test.txt") do (
for %%x in (%%J) do (
echo(%%I,%%x
)
)
Output:
AAA,BBB
AAA,CCC
DDD,EEE
FFF,GGG
HHH,III
HHH,JJJ
There have already been great answers provided with some smart approaches.
However, I want to stick to the code you posted here.
The main problem is the line if %%c EQU [], because it compares the third token with the literal string []; the third token can be CCC, JJJ, or an empty string, according to your example, so the condition is never going to be fulfilled.
To correct that, you should write if "%%c"=="" instead.
You could further improve your script by doing a single redirection > to the output file rather than creating it in advance and appending multiple times; just put the entire for /F loop in between parentheses and redirect the whole block.
So here is the corrected and improved code:
rem Transfer s_result.txt to sf_result.txt
> "sf_result.txt" (
for /F "usebackq tokens=1-3 delims=," %%a in ("s_result.txt") do #(
if "%%c"=="" (
echo %%a,%%b
) else (
echo %%a,%%b
echo %%a,%%c
)
)
)
The # symbol prevents command echoes of the loop body to be written to the output file as well. If there is #echo off placed at the beginning of your script you do no longer need that symbol.
Of course this code cannot handle lines with more than three tokens correctly.
I do like rojos clever approach (+1).
To overcome the implications he mentions I think of a recursive approach.
:: Q:\Test\2018\06\28\SO_51073893.cmd
#echo off & setlocal
for /f "usebackq tokens=1-2* delims=," %%A in ("test.txt") do Call :Sub "%%A" "%%B" "%%C"
Goto :Eof
:Sub
Echo %~1,%~2
if "%~3" neq "" for /f "tokens=1* delims=," %%D in (%3) do Call :Sub %1 "%%D" "%%E"
With a slightly changed file test.txt I get this output:
> SO_51073893.cmd
AAA,B=B
AAA,C;C
DDD,EEE
FFF,GGG
HHH,I I
HHH,JJJ

Locating keyword in .csv files

I am trying to create batch file that reads specific CSV documents from specific file, and extracts some lines that have specific number and print it out on the screen " the whole line !". The problem is I created the code but it wont work at all, whenever I tried it only prints the line numbers!?
The code:
#echo off
setlocal EnableDelayedExpansion
set "yourDir=C:\Users\Adminm\Desktop\test11\"
set "yourExt=csv"
set "keyword=44"
set /a count=0
set linenum=!count!
set c=0
pushd %yourDir%
for %%a in (*.%yourExt%) do (
for /f "usebackq tokens=3 delims=," %%b in (%yourDir%%%a) do (
set /a count = !count! + 1
if NOT %%b == %keyword% (
for /f "delims=" %%1 in ('type %yourDir%%%a') do (
set /a c+=1 && if "!c!" equ "%linenum%" echo %%1%
)
)
)
)
echo !count!
popd
endlocal
thanks in advance <3
for %%a in (*.%yourExt%) do (
for /f "usebackq delims=" %%L in ("%%a") do (
for /f "tokens=3 delims=," %%b in ("%%L") do (
if %%b == %keyword% echo %%L
)
)
)
Assuming what you want to do is scan each file for a target string in column3, then:
Since you have already changed to yourdir, there's no requirement to specify it in the scan-for-filenames for.
Your attempt to locate the required line is clumsy. All you need to do is assign each line in turn to a metavariable (%%L) and then use for/ to parse the metavariable. When the required data matches, simply echo the metavariable containing the entire line.
You've attempted to use %%1 as a metavariable. %n for n=0..9 refers to the parameter number supplied to the routine. The only officially defined metavariables for use here are %%a..%%z and %%A..%%Z (one of the very few places where batch is case-sensitive) - although some other symbols also work. Numerics will not work here.

Batch script to read a specific line from a log file

I am trying to write a batch script that reads 18th line from a .log file and outputs the same. The .log file name varies each time. abc_XXXX.log where xxxx are process IDs. Below is the code I am trying to run to achieve this.
:Test1
set "xprvar=" for /F "skip=17 delims=" %%p in (abc*.log) do (echo %%p& goto
break)
:break
pause
goto END
set var=anyCommand doesn't work. It just sets the var to the literal string.
The usage of afor /f is the right way, just the variable assignment works different:
for /F "skip=17 delims=" %%p in ('dir /b abc*.log') do ( set "xprvar=%%p"& goto break )
There is also an option using FindStr
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr/N "^" "abc_*.log" 2^>Nul'
) Do If %%B Equ 18 Echo %%A:%%C
Pause
The above example Echoes the <filename>:<18th line content>, but there's no reason in the appropriate situation why you couldn't change that to read:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr/N "^" "abc_*.log" 2^>Nul'
) Do If %%B Equ 18 Set "xprvar=%%C"
If there is more than one matching filename in the directory, the variable would be set to the content in the last file parsed.
#ECHO Off
SETLOCAL
FOR %%f IN (abc*.log) DO (
SET "reported="
FOR /f "skip=17delims=" %%p IN (%%f) DO IF NOT DEFINED reported (
ECHO %%p
SET "reported=Y"
)
)
Assign each filename in turn to %%f.
For each filename found, clear the reported flag then read the file, skipping the first 17 lines. echo the 18th line and set the reported flag so that the remainder of the lines are not echoed.

Batch Parse Filename to CSV

I have a folder of 9000 files named XX-randomString-numbers.jpg, where XX is an ID number I need.
I need to export it to a CSV list of [ XX | filename.jpg ] so that I can work with it in SQL.
So far, I have pieced this together:
Setlocal Enabledelayedexpansion
for %%A in ("C:\...\imageImport\*.jpg")
do (
set "str=%%~nA"
for /f "delims=-" %%a in ("%str%")
do set part=%%a
echo.%part%
)
#Echo off
Setlocal Enabledelayedexpansion
( Echo ID^|Filename
for %%F in ("C:\...\imageImport\*.jpg"
) do for /f "delims=-" %%I in ("%%F"
) do set Echo %%I^|%%F
) >Out.csv
You are enabling delayed expansion but you are not using it. Anyway, you do not need it in your code, because you do not really need any interim variables. What you do need though is to correct the syntax of the for loops and remove the superfluous line-breaks in front of do.
So here is the fixed code:
for %%A in ("C:\...\imageImport\*.jpg") do (
for /F "delims=- eol=-" %%a in ("%%~nA") do (
echo(%%a^|%%~nA
)
)
To write the result into a file output.csv, use this:
#echo off
> "output.csv" (
for %%A in ("C:\...\imageImport\*.jpg") do (
for /F "delims=- eol=-" %%a in ("%%~nA") do (
echo(%%a^|%%~nA
)
)
)
The eol=- option is specified for the for /F loop not to ignore files beginning with the default eol character ; and so, the eol option is effectively deactivated, because eol becomes hidden behind delims as the same character is given.

Extract Part of a text file in BAT

I am capturing a m3U file on a daily basis but wish to parse part of it to another file with the few channels I need.
For example I have renamed my m3U to Test.txt file which say has the following fictional structure:
#EXTINF:0,ABC
#live link 1
#EXTINF:0,XYZ
#live link 2
#EXTINF:0,UVW
#live link 3
I would just like to capture say the line staring from "#EXTINF:0,XYZ" and say the line beneath it to end up with a Output.txt as follows:
#EXTINF:0,XYZ
#live link 2
I know that one needs to use the For loop but I am a bit of a noob on this area.
Put this code into the file filter.cmd.
#echo off
set INPUT=%1&set MATCH=%2& set MATCHED=0
for /f "delims=" %%a in (%INPUT%) do call :line "%%~a"
goto :eof
:line
set EXT=&TITLE=&
for /f "tokens=1 delims=:" %%a in ("%~1") do set EXT=%%~a
for /f "tokens=1,2,* delims=:," %%a in ("%~1") do set TITLE=%%~c
if "%EXT%" == "#EXTM3U" echo %~1
if "%EXT%" == "#EXTINF" (
set MATCHED=0
echo %TITLE%| findstr /l %MATCH% >nul && set MATCHED=1
)
if %MATCHED%==1 echo %~1
Use example:
filter.cmd input_file.m3u XYZ > output_file.m3u
Here is some explanation:
Every input line is split using for /f with tokens and delims.
MATCHED is set if the line begins with #EXTINF and the rest contains the string to match (second argument).
if MATCHED is set, the lines are output until next #EXTINF.
I would do it like this, supposing the .m3u file does not contain trailing white-spaces in the lines preceded by #EXTINF, like your sample data does:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILE=%~1"
set "HEADER=#EXTM3U"
set "PREFIX=#EXTINF"
set "MATCH=%~2"
set "FLAG="
for /F usebackq^ delims^=^ eol^= %%L in ("%FILE%") do (
if defined FLAG (
echo(%%L
set "FLAG="
)
for /F "delims=:" %%P in ("%%L") do (
if "%%P"=="%HEADER%" (
echo(%%L
) else if "%%P"=="%PREFIX%" (
set "LINE=%%L"
setlocal EnableDelayedExpansion
if /I "!LINE:*,=!"=="!MATCH!" (
echo(!LINE!
endlocal
set "FLAG=#"
) else endlocal
)
)
)
endlocal
exit /B
Call the script like this, supposing it is saved as extract-entry.bat:
extract-entry.bat "input_file.m3u" "XYZ" > "output_file.m3u"
The script walks through the given .m3u file line by line. It returns the current line unedited and resets variable FLAG, if variable FLAG is set, which is not the case at the beginning.
Then it looks for #EXTINF. If found (e. g., #EXTINF:0,XYZ), the string after the comma (XYZ) is compared against the given search string. If matched, the current line is output and FLAG variable is set now in order to get the following line too.
The header line #EXTM3U is always output.
Toggling delayed expansion makes this script robust against all characters that have special meaning to the command interpreter without losing them.

Resources