Batch file escaping issuesEscaping HTML strings with jQueryHow can I pass arguments to a batch file?Batch file to delete files older than N daysSplit long commands in multiple lines through Windows batch fileHow can I echo a newline in a batch file?Windows batch files: .bat vs .cmd?What characters do I need to escape in XML documents?How to sleep for five seconds in a batch file/cmdWant six instances of command prompt window to be running at any point of time unless all the 100 commands are completedBatch commands to swap text from file
Find the closest three-digit hex colour
Why is my 401k manager recommending me to save more?
Advantages of using bra-ket notation
tikz: draw multicolor curve with smooth gradient
Would for willingness in the past
Why would Dementors torture a Death Eater if they are loyal to Voldemort?
What's the lunar calendar of two moons
Is it possible to alias a column based on the result of a select+where?
Is there a word for the act of simultaneously pulling and twisting an object?
How much of a mortgage should I take on to maximize my 5 year financial plan?
Robots in a spaceship
Why isn't UDP with reliability (implemented at Application layer) a substitute of TCP?
Why are examinees often not allowed to leave during the start and end of an exam?
Does a lens with a bigger max. aperture focus faster than a lens with a smaller max. aperture?
English idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)
What is this fluorinated organic substance?
How is it possible for tall trees to pull water to heights more than 10m?
Simplify the code
Any Tips On Writing Extended Recollection In A Novel
Does friction always oppose motion?
What would you need merely the term "collection" for pitches, but not "scale"?
Avoiding repetition when using the "snprintf idiom" to write text
What are the children of two Muggle-borns called?
What happens if a caster is surprised while casting a spell with a long casting time?
Batch file escaping issues
Escaping HTML strings with jQueryHow can I pass arguments to a batch file?Batch file to delete files older than N daysSplit long commands in multiple lines through Windows batch fileHow can I echo a newline in a batch file?Windows batch files: .bat vs .cmd?What characters do I need to escape in XML documents?How to sleep for five seconds in a batch file/cmdWant six instances of command prompt window to be running at any point of time unless all the 100 commands are completedBatch commands to swap text from file
I've written a batch file to modify the attribute of an XML file. The script works and the attribute if modified, however I'm having an issue with escaping some characters.
I've tried every solution I can possibly find online with no luck.
My intended output is:
<xs:import schemaLocation="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd" namespace="urn:verastar:veracore:types" />
However I'm getting:
<xs:import schemaLocation="="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd"" namespace="urn:verastar:veracore:types
I've tried escaping the quotes with ^
, this works when using echo to the console however doesn;t work when writing to the file.
Why am I getting ="="
also I'm getting double quotes, but when I remove one nothing gets written to the file.. and finally the last quote seems to be messing things up and therefore the output is missing the XML closing tag.
How can I escape these characters properly?
My code is:
@echo on
setlocal EnableExtensions DisableDelayedExpansion
rem check if the XSD to modify exists in the batch directory
set "XSDFile=%~dp0test.xsd"
if not exist "%XSDFile%" goto EndBatch
rem environment variables
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%%~n0.tmp"
rem Search for the line containing attribute schemaLocation and get its
rem line number and the line itself loaded into environment variables
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /L /N /C:schemaLocation= "%XSDFile%" 2^>nul') do (
set "LineNumber=%%I"
set "FileLine=%%J"
)
rem If no line with attribute schemaLocation found, exit this batch file
if not defined LineNumber goto EndBatch
setlocal EnableDelayedExpansion
set "FileName=!FileLine:*schemaLocation=!"
for /f "tokens=1 delims=?" %%a in ("%FileName%") do (set test=%%a)
set "test=<xs:import schemaLocation="%test%.xsd"" namespace="urn:verastar:veracore:types" />"
pause
endlocal & set "FileLine=%test%
rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul
rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute schemaLocation
rem which is copied to temporary file with the modified schemaLocation.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /R /N "^" "%XSDFile%" 2^>nul') do (
set "XmlLine=%%J"
set /A LineCount+=1
setlocal EnableDelayedExpansion
if not !LineCount! == %LineNumber% (
echo/!XmlLine!>>"%TmpFile%"
) else (
echo/!FileLine!>>"%TmpFile%"
)
endlocal
)
rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XSDFile%" >nul
:EndBatch
endlocal
batch-file escaping
|
show 2 more comments
I've written a batch file to modify the attribute of an XML file. The script works and the attribute if modified, however I'm having an issue with escaping some characters.
I've tried every solution I can possibly find online with no luck.
My intended output is:
<xs:import schemaLocation="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd" namespace="urn:verastar:veracore:types" />
However I'm getting:
<xs:import schemaLocation="="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd"" namespace="urn:verastar:veracore:types
I've tried escaping the quotes with ^
, this works when using echo to the console however doesn;t work when writing to the file.
Why am I getting ="="
also I'm getting double quotes, but when I remove one nothing gets written to the file.. and finally the last quote seems to be messing things up and therefore the output is missing the XML closing tag.
How can I escape these characters properly?
My code is:
@echo on
setlocal EnableExtensions DisableDelayedExpansion
rem check if the XSD to modify exists in the batch directory
set "XSDFile=%~dp0test.xsd"
if not exist "%XSDFile%" goto EndBatch
rem environment variables
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%%~n0.tmp"
rem Search for the line containing attribute schemaLocation and get its
rem line number and the line itself loaded into environment variables
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /L /N /C:schemaLocation= "%XSDFile%" 2^>nul') do (
set "LineNumber=%%I"
set "FileLine=%%J"
)
rem If no line with attribute schemaLocation found, exit this batch file
if not defined LineNumber goto EndBatch
setlocal EnableDelayedExpansion
set "FileName=!FileLine:*schemaLocation=!"
for /f "tokens=1 delims=?" %%a in ("%FileName%") do (set test=%%a)
set "test=<xs:import schemaLocation="%test%.xsd"" namespace="urn:verastar:veracore:types" />"
pause
endlocal & set "FileLine=%test%
rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul
rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute schemaLocation
rem which is copied to temporary file with the modified schemaLocation.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /R /N "^" "%XSDFile%" 2^>nul') do (
set "XmlLine=%%J"
set /A LineCount+=1
setlocal EnableDelayedExpansion
if not !LineCount! == %LineNumber% (
echo/!XmlLine!>>"%TmpFile%"
) else (
echo/!FileLine!>>"%TmpFile%"
)
endlocal
)
rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XSDFile%" >nul
:EndBatch
endlocal
batch-file escaping
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
sorry, that statement was a bit vaque. You're doing replace,*schemalocation=!
meaning everything gets replaced before the=
as the=
is used for replacement, so you never replace it. so you should simply remove the=
from the set line..set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
Thanks for the info: now my result is just="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag
– DNKROZ
Mar 25 at 17:35
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05
|
show 2 more comments
I've written a batch file to modify the attribute of an XML file. The script works and the attribute if modified, however I'm having an issue with escaping some characters.
I've tried every solution I can possibly find online with no luck.
My intended output is:
<xs:import schemaLocation="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd" namespace="urn:verastar:veracore:types" />
However I'm getting:
<xs:import schemaLocation="="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd"" namespace="urn:verastar:veracore:types
I've tried escaping the quotes with ^
, this works when using echo to the console however doesn;t work when writing to the file.
Why am I getting ="="
also I'm getting double quotes, but when I remove one nothing gets written to the file.. and finally the last quote seems to be messing things up and therefore the output is missing the XML closing tag.
How can I escape these characters properly?
My code is:
@echo on
setlocal EnableExtensions DisableDelayedExpansion
rem check if the XSD to modify exists in the batch directory
set "XSDFile=%~dp0test.xsd"
if not exist "%XSDFile%" goto EndBatch
rem environment variables
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%%~n0.tmp"
rem Search for the line containing attribute schemaLocation and get its
rem line number and the line itself loaded into environment variables
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /L /N /C:schemaLocation= "%XSDFile%" 2^>nul') do (
set "LineNumber=%%I"
set "FileLine=%%J"
)
rem If no line with attribute schemaLocation found, exit this batch file
if not defined LineNumber goto EndBatch
setlocal EnableDelayedExpansion
set "FileName=!FileLine:*schemaLocation=!"
for /f "tokens=1 delims=?" %%a in ("%FileName%") do (set test=%%a)
set "test=<xs:import schemaLocation="%test%.xsd"" namespace="urn:verastar:veracore:types" />"
pause
endlocal & set "FileLine=%test%
rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul
rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute schemaLocation
rem which is copied to temporary file with the modified schemaLocation.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /R /N "^" "%XSDFile%" 2^>nul') do (
set "XmlLine=%%J"
set /A LineCount+=1
setlocal EnableDelayedExpansion
if not !LineCount! == %LineNumber% (
echo/!XmlLine!>>"%TmpFile%"
) else (
echo/!FileLine!>>"%TmpFile%"
)
endlocal
)
rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XSDFile%" >nul
:EndBatch
endlocal
batch-file escaping
I've written a batch file to modify the attribute of an XML file. The script works and the attribute if modified, however I'm having an issue with escaping some characters.
I've tried every solution I can possibly find online with no luck.
My intended output is:
<xs:import schemaLocation="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd" namespace="urn:verastar:veracore:types" />
However I'm getting:
<xs:import schemaLocation="="2e9dd7db-f58b-4c91-8575-3b3af05d3178.xsd"" namespace="urn:verastar:veracore:types
I've tried escaping the quotes with ^
, this works when using echo to the console however doesn;t work when writing to the file.
Why am I getting ="="
also I'm getting double quotes, but when I remove one nothing gets written to the file.. and finally the last quote seems to be messing things up and therefore the output is missing the XML closing tag.
How can I escape these characters properly?
My code is:
@echo on
setlocal EnableExtensions DisableDelayedExpansion
rem check if the XSD to modify exists in the batch directory
set "XSDFile=%~dp0test.xsd"
if not exist "%XSDFile%" goto EndBatch
rem environment variables
set "LineNumber="
set "LineCount=0"
set "TmpFile=%TEMP%%~n0.tmp"
rem Search for the line containing attribute schemaLocation and get its
rem line number and the line itself loaded into environment variables
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /L /N /C:schemaLocation= "%XSDFile%" 2^>nul') do (
set "LineNumber=%%I"
set "FileLine=%%J"
)
rem If no line with attribute schemaLocation found, exit this batch file
if not defined LineNumber goto EndBatch
setlocal EnableDelayedExpansion
set "FileName=!FileLine:*schemaLocation=!"
for /f "tokens=1 delims=?" %%a in ("%FileName%") do (set test=%%a)
set "test=<xs:import schemaLocation="%test%.xsd"" namespace="urn:verastar:veracore:types" />"
pause
endlocal & set "FileLine=%test%
rem Make sure the temporary file used next does not already exist.
del "%TmpFile%" 2>nul
rem Copy all lines from XML file to a temporary file including empty
rem lines with the exception of the line containing attribute schemaLocation
rem which is copied to temporary file with the modified schemaLocation.
for /F "tokens=1* delims=:" %%I in ('%SystemRoot%System32findstr.exe /R /N "^" "%XSDFile%" 2^>nul') do (
set "XmlLine=%%J"
set /A LineCount+=1
setlocal EnableDelayedExpansion
if not !LineCount! == %LineNumber% (
echo/!XmlLine!>>"%TmpFile%"
) else (
echo/!FileLine!>>"%TmpFile%"
)
endlocal
)
rem Overwrite original file with temporary file automatically deleted on success.
move /Y "%TmpFile%" "%XSDFile%" >nul
:EndBatch
endlocal
batch-file escaping
batch-file escaping
edited Mar 25 at 16:55
DNKROZ
asked Mar 25 at 16:33
DNKROZDNKROZ
1,7013 gold badges17 silver badges33 bronze badges
1,7013 gold badges17 silver badges33 bronze badges
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
sorry, that statement was a bit vaque. You're doing replace,*schemalocation=!
meaning everything gets replaced before the=
as the=
is used for replacement, so you never replace it. so you should simply remove the=
from the set line..set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
Thanks for the info: now my result is just="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag
– DNKROZ
Mar 25 at 17:35
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05
|
show 2 more comments
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
sorry, that statement was a bit vaque. You're doing replace,*schemalocation=!
meaning everything gets replaced before the=
as the=
is used for replacement, so you never replace it. so you should simply remove the=
from the set line..set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
Thanks for the info: now my result is just="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag
– DNKROZ
Mar 25 at 17:35
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
sorry, that statement was a bit vaque. You're doing replace,
*schemalocation=!
meaning everything gets replaced before the =
as the =
is used for replacement, so you never replace it. so you should simply remove the =
from the set line.. set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
sorry, that statement was a bit vaque. You're doing replace,
*schemalocation=!
meaning everything gets replaced before the =
as the =
is used for replacement, so you never replace it. so you should simply remove the =
from the set line.. set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
Thanks for the info: now my result is just
="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag– DNKROZ
Mar 25 at 17:35
Thanks for the info: now my result is just
="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag– DNKROZ
Mar 25 at 17:35
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342462%2fbatch-file-escaping-issues%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342462%2fbatch-file-escaping-issues%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
because you're attempting to replace an equal sign using standard saearch replace..
– Gerhard Barnard
Mar 25 at 17:14
@GerhardBarnard which part do you mean?
– DNKROZ
Mar 25 at 17:18
sorry, that statement was a bit vaque. You're doing replace,
*schemalocation=!
meaning everything gets replaced before the=
as the=
is used for replacement, so you never replace it. so you should simply remove the=
from the set line..set "test=<xs:import schemaLocation"%test%.xsd"" namespace="urn:verastar:veracore:types" />"
– Gerhard Barnard
Mar 25 at 17:28
Thanks for the info: now my result is just
="b541ba91-4125-46ec-8058-f0c983c80a72.xsd" namespace="urn:verastar:veracore:types
which is missing the start of the XML tag and the closing tag– DNKROZ
Mar 25 at 17:35
set test=^<xs:import schemaLocation%test%.xsd" namespace="urn:verastar:veracore:types" /^>
– Gerhard Barnard
Mar 25 at 18:05