

InterviewSolution
Saved Bookmarks
1. |
Solve : How do I check if the parameter %1 exist in a batch file (IF statement)?? |
Answer» <html><body><p>I tried the following batch file:<br/><br/>ECHO OFF<br/>:start<br/>Echo - %1<br/>shift<br/>IF %1=="" exit<br/>pause<br/>goto start<br/><br/>and run it as shift.bat 1 2 3<br/><br/>After the 3rd parameter, I want the program to exit. However, the IF statement <br/>IF %1=="" exit<br/>is incorrect. I also tried <br/>IF %1=="" echo - Missing<br/>but this was also incorrect...<br/><br/>What is the correct syntax to evaluate if %1 is missing?both sides need to evaluate to something; in your code, once you get to the third parameter, the if <a href="https://interviewquestions.tuteehub.com/tag/becomes-894381" style="font-weight:bold;" target="_blank" title="Click to know more about BECOMES">BECOMES</a><br/> Code: <a>[Select]</a>IF =="" EXIT<br/>which is invalid syntax.<br/><br/>use:<br/> Code: <a>[Select]</a>IF "%1"=="" EXIT<br/>Of course. Need to "define" the %1 as a string.<br/>Thanks! Quote from: viking2 on February 28, 2010, 02:25:28 PM</p><blockquote>Of course. Need to "define" the %1 as a string.<br/>Thanks!<br/></blockquote> well, not really... you could just as easily do<br/><br/> Code: <a>[Select]</a>IF !%1==! EXIT<br/><br/>It's just when it expands so there is nothing on one side, it's not a valid syntax. Quote from: viking2 on February 28, 2010, 01:27:58 PM<blockquote>I tried the following batch file:<br/>What is the correct syntax to evaluate if %1 is missing?<br/></blockquote> <br/><strong>Quotes around "%1" worked </strong> <br/><br/>C:\batch>type shift.bat<br/><br/> Code: <a>[Select]</a>ECHO OFF<br/>:start<br/>Echo - %1<br/>shift<br/>IF "%1"=="" exit /<a href="https://interviewquestions.tuteehub.com/tag/b-236590" style="font-weight:bold;" target="_blank" title="Click to know more about B">B</a><br/>pause<br/>goto start<br/><br/><strong>Output:</strong><br/><br/>C:\batch>shift.bat 1 2 3 4<br/>- 1<br/>Press any key to continue . . .<br/>- 2<br/>Press any key to continue . . .<br/>- 3<br/>Press any key to continue . . .<br/>- 4<br/><br/>C:\batch><br/><br/>p.s.: use exit /b to exit the batch and not exit the command <a href="https://interviewquestions.tuteehub.com/tag/prompt-592976" style="font-weight:bold;" target="_blank" title="Click to know more about PROMPT">PROMPT</a> <a href="https://interviewquestions.tuteehub.com/tag/page-25452" style="font-weight:bold;" target="_blank" title="Click to know more about PAGE">PAGE</a>.</body></html> | |