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.


Thread locked

Messages in this Thread

95,060 Postings in 11,983 Threads, 350 registered users, 40 users online (1 registered, 39 guests)
Index | Admin contact |   Forum Time: Aug 3, 2020 - 7:33 am EDT  |  Hits:32,786,570  (9,771 Today )
RSS Feed