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

201 views      
Thread locked
 

Messages in this Thread

 
94,593 Postings in 11,933 Threads, 348 registered users, 20 users online (0 registered, 20 guests)
Index | Admin contact |   Forum Time: Nov 22, 2019 - 8:01 pm EST  |  Hits:29,239,576  (14,254 Today )
RSS Feed