ZTree.com  | ZEN  | About...  

 Index   Back

put "" around an IF term - other issues   [Q]

By: Hartmut Schneider     Germany  
Date: Oct 09,2018 at 02:32
In Response to: [Q] REM #ZTStart "" behavior changes or something else like a bug? (Nick Williams)

> It's good coding practice to ALWAYS use double quotes in IF statements
> -- even when you feel sure that your environment variables will never
> contain spaces or other argument separator characters (delimiters), e.g.
> comma, semicolon, equals, space, tab. Also, characters like the pipe
> symbol, ampersand and less-than and greater-than need to be within double
> quotes.

Agree. It generally avoids this syntax error in the IF statement.

But what to do if the %VAR% contents itself starts and ends with double quotes?
For example, IF "file name"==""file name"" never returns true.

This situation arises when passing a filename to IF.
ZTree for example adds surrounding quotes to a filename only if it contains spaces or other argument separator characters.
Fortunately, ZTree does not surround when path/file params like %4.%5 are preceded by any non-space character.
Example: echo "%4.%5" has only one pair of quotes.

In case you get problems in other situations when writing CMD batch files:
I know of two methods to strip surrounding quotes on the command shell level:
(1) %~1 (see call /?) - works only when passing parameters to a batch (or a :sub within a batch)

(2) %VAR:str1=str2% (see set /?), in this case %VAR:"=% - i.e. substitute '=' with nothing.


Hartmut

82 views      
 

Messages in this Thread

 
93,933 Postings in 11,850 Threads, 347 registered users, 10 users online (1 registered, 9 guests)
Index | Admin contact |   Forum Time: Dec 10, 2018 - 12:05 pm EST  |  Hits:26,925,610  (2,078 Today )
RSS Feed