Surrounding quoted parameter with space in the batch script argument












0














I'm facing one issue when passing surrounding quoted parameter with space in the batch script argument. Here is my code and the output, please let me know what is missing.



Here is my script:



:mainFunction
:loop
ECHO key %1 and value %2
IF NOT %1=="" (
ECHO User has provided parameter.
IF "%1"=="-installDir" (
IF "%2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


and here is the output (with key -installDir and value "D:TestNew Folder"):



C:UsersTestDesktopBatchPro>installer.bat -installDir="D:TestNew Folder" -addToDesktop="true"
Folder""=="" was unexpected at this time.
C:UsersSinhaDesktopBatchPro>


So please tell me why this error is coming even after providing parameter inside double quotes.










share|improve this question
























  • try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
    – npocmaka
    Nov 12 at 11:49












  • You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
    – Compo
    Nov 12 at 11:52










  • @npocmaka I tried with "%~1"=="", it didn't work.
    – Dams
    Nov 12 at 11:56










  • @ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
    – Dams
    Nov 12 at 11:58










  • @Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
    – Dams
    Nov 12 at 12:02
















0














I'm facing one issue when passing surrounding quoted parameter with space in the batch script argument. Here is my code and the output, please let me know what is missing.



Here is my script:



:mainFunction
:loop
ECHO key %1 and value %2
IF NOT %1=="" (
ECHO User has provided parameter.
IF "%1"=="-installDir" (
IF "%2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


and here is the output (with key -installDir and value "D:TestNew Folder"):



C:UsersTestDesktopBatchPro>installer.bat -installDir="D:TestNew Folder" -addToDesktop="true"
Folder""=="" was unexpected at this time.
C:UsersSinhaDesktopBatchPro>


So please tell me why this error is coming even after providing parameter inside double quotes.










share|improve this question
























  • try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
    – npocmaka
    Nov 12 at 11:49












  • You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
    – Compo
    Nov 12 at 11:52










  • @npocmaka I tried with "%~1"=="", it didn't work.
    – Dams
    Nov 12 at 11:56










  • @ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
    – Dams
    Nov 12 at 11:58










  • @Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
    – Dams
    Nov 12 at 12:02














0












0








0







I'm facing one issue when passing surrounding quoted parameter with space in the batch script argument. Here is my code and the output, please let me know what is missing.



Here is my script:



:mainFunction
:loop
ECHO key %1 and value %2
IF NOT %1=="" (
ECHO User has provided parameter.
IF "%1"=="-installDir" (
IF "%2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


and here is the output (with key -installDir and value "D:TestNew Folder"):



C:UsersTestDesktopBatchPro>installer.bat -installDir="D:TestNew Folder" -addToDesktop="true"
Folder""=="" was unexpected at this time.
C:UsersSinhaDesktopBatchPro>


So please tell me why this error is coming even after providing parameter inside double quotes.










share|improve this question















I'm facing one issue when passing surrounding quoted parameter with space in the batch script argument. Here is my code and the output, please let me know what is missing.



Here is my script:



:mainFunction
:loop
ECHO key %1 and value %2
IF NOT %1=="" (
ECHO User has provided parameter.
IF "%1"=="-installDir" (
IF "%2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


and here is the output (with key -installDir and value "D:TestNew Folder"):



C:UsersTestDesktopBatchPro>installer.bat -installDir="D:TestNew Folder" -addToDesktop="true"
Folder""=="" was unexpected at this time.
C:UsersSinhaDesktopBatchPro>


So please tell me why this error is coming even after providing parameter inside double quotes.







batch-file command-line-arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 12:28









LotPings

17.2k61531




17.2k61531










asked Nov 12 at 11:41









Dams

93




93












  • try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
    – npocmaka
    Nov 12 at 11:49












  • You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
    – Compo
    Nov 12 at 11:52










  • @npocmaka I tried with "%~1"=="", it didn't work.
    – Dams
    Nov 12 at 11:56










  • @ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
    – Dams
    Nov 12 at 11:58










  • @Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
    – Dams
    Nov 12 at 12:02


















  • try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
    – npocmaka
    Nov 12 at 11:49












  • You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
    – Compo
    Nov 12 at 11:52










  • @npocmaka I tried with "%~1"=="", it didn't work.
    – Dams
    Nov 12 at 11:56










  • @ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
    – Dams
    Nov 12 at 11:58










  • @Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
    – Dams
    Nov 12 at 12:02
















try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
– npocmaka
Nov 12 at 11:49






try with IF NOT "%~1"=="" . Mind that = is a delimiter in the batch files and -installDir and "D:TestNew Folder" will be taken for two different parameters.
– npocmaka
Nov 12 at 11:49














You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
– Compo
Nov 12 at 11:52




You should use the following syntax, If "%~1"=="-installDir" ( and If "%~2"=="" (. The ~ expands removing any surrounding doublequotes.
– Compo
Nov 12 at 11:52












@npocmaka I tried with "%~1"=="", it didn't work.
– Dams
Nov 12 at 11:56




@npocmaka I tried with "%~1"=="", it didn't work.
– Dams
Nov 12 at 11:56












@ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
– Dams
Nov 12 at 11:58




@ Gerhard Barnard It did not reach till that line where it checks for %2=="". If i'm getting correct value with %1 and %2, then why this error is coming.
– Dams
Nov 12 at 11:58












@Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
– Dams
Nov 12 at 12:02




@Compo If "%~1"=="-installDir" ( and If "%~2"=="" also not working. Still getting same error.
– Dams
Nov 12 at 12:02












1 Answer
1






active

oldest

votes


















0














I took what was said in the comments and created an answer. Please give this a try:



:mainFunction
@echo off
:loop
ECHO key %1 and value %2
IF NOT "%~1"=="" (
ECHO User has provided parameter.
IF "%~1"=="-installDir" (
IF "%~2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


You can run for /? from cmd terminal and read under the "variable substitution" section:



%~I         - expands %I removing any surrounding quotes (")





share|improve this answer





















  • Yeah..its working. thanks a lot Gerhard Barnard.
    – Dams
    Nov 12 at 12:25










  • My Pleasure....
    – Gerhard Barnard
    Nov 12 at 12:43











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261416%2fsurrounding-quoted-parameter-with-space-in-the-batch-script-argument%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I took what was said in the comments and created an answer. Please give this a try:



:mainFunction
@echo off
:loop
ECHO key %1 and value %2
IF NOT "%~1"=="" (
ECHO User has provided parameter.
IF "%~1"=="-installDir" (
IF "%~2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


You can run for /? from cmd terminal and read under the "variable substitution" section:



%~I         - expands %I removing any surrounding quotes (")





share|improve this answer





















  • Yeah..its working. thanks a lot Gerhard Barnard.
    – Dams
    Nov 12 at 12:25










  • My Pleasure....
    – Gerhard Barnard
    Nov 12 at 12:43
















0














I took what was said in the comments and created an answer. Please give this a try:



:mainFunction
@echo off
:loop
ECHO key %1 and value %2
IF NOT "%~1"=="" (
ECHO User has provided parameter.
IF "%~1"=="-installDir" (
IF "%~2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


You can run for /? from cmd terminal and read under the "variable substitution" section:



%~I         - expands %I removing any surrounding quotes (")





share|improve this answer





















  • Yeah..its working. thanks a lot Gerhard Barnard.
    – Dams
    Nov 12 at 12:25










  • My Pleasure....
    – Gerhard Barnard
    Nov 12 at 12:43














0












0








0






I took what was said in the comments and created an answer. Please give this a try:



:mainFunction
@echo off
:loop
ECHO key %1 and value %2
IF NOT "%~1"=="" (
ECHO User has provided parameter.
IF "%~1"=="-installDir" (
IF "%~2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


You can run for /? from cmd terminal and read under the "variable substitution" section:



%~I         - expands %I removing any surrounding quotes (")





share|improve this answer












I took what was said in the comments and created an answer. Please give this a try:



:mainFunction
@echo off
:loop
ECHO key %1 and value %2
IF NOT "%~1"=="" (
ECHO User has provided parameter.
IF "%~1"=="-installDir" (
IF "%~2"=="" (
ECHO Invalid value.
EXIT /B 0
) ELSE (
ECHO Valid value.
SHIFT
)
)
REM Check other parameters and parse them.
SHIFT
GOTO :loop
)
EXIT /B %ERRORLEVEL%


You can run for /? from cmd terminal and read under the "variable substitution" section:



%~I         - expands %I removing any surrounding quotes (")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 12:13









Gerhard Barnard

6,90631131




6,90631131












  • Yeah..its working. thanks a lot Gerhard Barnard.
    – Dams
    Nov 12 at 12:25










  • My Pleasure....
    – Gerhard Barnard
    Nov 12 at 12:43


















  • Yeah..its working. thanks a lot Gerhard Barnard.
    – Dams
    Nov 12 at 12:25










  • My Pleasure....
    – Gerhard Barnard
    Nov 12 at 12:43
















Yeah..its working. thanks a lot Gerhard Barnard.
– Dams
Nov 12 at 12:25




Yeah..its working. thanks a lot Gerhard Barnard.
– Dams
Nov 12 at 12:25












My Pleasure....
– Gerhard Barnard
Nov 12 at 12:43




My Pleasure....
– Gerhard Barnard
Nov 12 at 12:43


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53261416%2fsurrounding-quoted-parameter-with-space-in-the-batch-script-argument%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python