Reputation: 26
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
Upvotes: 0
Views: 385
Reputation: 26
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!
)
)
Upvotes: 0
Reputation: 26
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...
Upvotes: 0
Reputation: 56188
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)
Upvotes: 1