1

Please see the below code Example :

can anyone provide a solution???

I have a batch script that has few variables containing values such as strings and numbers. Sometimes, I am getting an empty string or a string containing only spaces as input. I want to deploy condition to filter out those cases.

 set Test_String = "   "

if Test_String=="empty/contains only spaces" 
//(do nothing )
else
//(do something)

I would like to get the solution for this to deploy in .bat file

1
  • You have had some good answers on your questions. What is the reason for not marking them as correct? If you feel any answer helped solve your issue you can upvote and select the answer by ticking the grey tock mark left of the answer. Commented Oct 10, 2019 at 12:34

2 Answers 2

4

Compo's IF solution with find/replace is probably the simplest and best performing method.

But FOR /F might be a good solution if you need to parse the value. For example, the default delims of space will effectively trim leading and trailing spaces.

FOR /F will not iterate a string if it is empty or contains only spaces/tabs. The EOL option should be set to space so that lines beginning with ; are not falsely missed (thanks Stephan for your comment). So if you want to do something with non-empty, but nothing upon empty (or blank), then

for /f "eol= " %%A in ("%VAR%") do (
  echo VAR is defined and contains more than just spaces or tabs
)

If you want to take action upon empty (or space value), then you can take advantage of the fact that FOR /F returns a non-zero value if it does not iterate. You must enclose the entire FOR construct in parentheses to properly detect the empty condition. And you should make sure the last command of the DO clause always returns 0.

(
  for /f "eol= " %%A in ("%VAR%") do (
    echo VAR is defined and contains more than just spaces or tabs
    rem Any number of commands here
    (call ) %= A quick way to force a return code of 0. The space after call is critical =%
  )
) || (
  echo VAR is not defined or contains only spaces/tabs
)
Sign up to request clarification or add additional context in comments.

5 Comments

Note: the value of var should not start with a ; (or any number of spaces followed by a ;)
@Stephan - Oops, thanks for pointing out that oversight. I've updated the answer.
ok, now I'm confused. (I expected "eol=") Doesn't eol define a (beginning) char to ignore the line? If it's set to <space> and the variable starts with a space, why is the do clause executed?
@Stephan - "EOL=" sets the EOL to a quote. Setting EOL to space works because DELIMS takes precedence over EOL. The leading spaces are stripped before EOL has a chance to see them. The only way to totally disable EOL is to use unquoted EOL^= at the end.
right - or any number of spaces followed by a ; should have told me that. :/ (thank you for the explanation) Or, as SS64 quotes: "'It's completely intuitive; it just takes a few days to learn, but then it's completely intuitive' - Terry Pratchett."
2

First thing you need to learn is how to set a variable using preferred/recommend syntax. Set "Test_String= " will define a variable named Test_String to a value of a single space, whereas set Test_String = " " will define the variable %Test_String % with a value of  " ".

You could perform a character replacement, replacing a space character with nothing, then your verification will be against an undefined/empty string:

@Set /P "Test_String=Please enter a string here to test against:"
@If "%Test_String: =%" == "" (Echo empty/contains only spaces) Else Echo do something
@Pause


A variable cannot have no value, it either has one or is not defined, so if you want to know whether the end user had made an entry at the prompt for input, then you could do it like this:

@Set "Test_String="
@Set /P "Test_String=Please enter a string here to test against:"
@If Not Defined Test_String (Echo no entry was made) Else Echo an entry was made
@Pause

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.