What is the proper way to test if a parameter is empty in a batch file?

blak3r

I need to test if a variable is set or not. I've tried several techniques but they seem to fail whenever %1 is surrounded by quotes such as the case when %1 is "c:\some path with spaces".

IF NOT %1 GOTO MyLabel // This is invalid syntax
IF "%1" == "" GOTO MyLabel // Works unless %1 has double quotes which fatally kills bat execution
IF %1 == GOTO MyLabel // Gives an unexpected GOTO error.

According to this site, these are the supported IF syntax types. So, I don't see a way to do it.

IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
Dan Story

Use square brackets instead of quotation marks:

IF [%1] == [] GOTO MyLabel

Parentheses are insecure: only use square brackets.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to test types?

What is the proper way to test mandatory field in selenium

what is the proper way to test NFS performance

What is the proper way to rename a file in Java?

What is the proper way to create a class instance with an empty dictionary and/or list?

What is the proper way to pass a cell as a parameter in a UDF in VBA?

What is the proper way to define a class when a function is an optional parameter?

What is the best way to test for an empty string in Go?

What's the proper way to test multiple constructors in Junit?

What is a proper way to test SQLAlchemy code that throw IntegrityError?

What is proper way to test if the input is Korean or Chinese using JavaScript?

What is the proper way to test a namespace-qualitified attribute name in a xsl:if?

What is a proper way to implement is_swappable to test for the Swappable concept?

What is the proper way to write a Minitest test for a method that yields?

What is the proper way to unit test Service with NestJS/Elastic

What is the proper way to unit test .NET Core Web Application?

What is the best way to do a substring in a batch file?

Is there a way to see what an executed batch file did?

What's the proper way to parse a very large JSON file in Ruby?

What is the proper way to format data in a .txt file? (Java)

What is the proper way to add a file to XYZ.csproj.FileListAbsolute.txt?

What is the proper way to parse the entries of a manifest.mf file in jar?

What is the proper way of preventing user interaction while downloading a file?

What is the proper way to mark a file as binary in Git Attributes?

What is the proper way to create a file in your server after the application starts?

What is the proper way to read numeric data (saved in a text file) into python?

What's the proper way to import variables from a different file?

What's the proper way to edit the text and the links of a menubar in a php file?

What is the proper way to load this edge-list file format?