ZTree.com  | ZEN  | About...  

 Index   Back

Understanding multiple percent signs   [Q]

By: Oliver Pretzel       
Date: Aug 30,2017 at 08:46
In Response to: adding 1 more %, doubling it, still dont work, so i was wondering if it was bug (Ryan)

Here is a brief explanation of why percent signs need to be doubled and also how many you need.

All DOS and Windows commandline batch processors, including ZTree's F9 menu interpret a single % as indicating that the enclosed text is the name or number of a variable or parameter (eg %myvariable% or %1). If you really want a percent sign you must 'escape' it by writing %%.

Why would you ever need to have %%? Well you might want to print something like
"Today's weather is 90% humidity". For that you need to write:
echo Today's weather is 90%% humidity

More common is when one batch file A writes another B. Each time that happens single % are interpreted as above and %% are replaced by %. So if you want B to call a variable %myvariable%, you must write %%myvariable%% in A. If you only write %myvariable% B will have whatever the value of myvariable is at the time of writing.

If you have A writing B which writes C then this happens twice so if you want C to contain %myvariable%, B must contain %%myvariable%% and A must contain %%%%myvariable%%%%.

ZTree's F9 Menu always writes a temporary batch file which is the called. So must normally double the number of percent signs you would need in a batch file.

Odd numbers of % signs are tricky.
If myvariable is set to xxx, and A writes B, then %%%myvariable%%% in A will produce %xxx% in B. If you understand why that is, you understand percent signs.


Thread locked

Messages in this Thread

94,975 Postings in 11,969 Threads, 349 registered users, 24 users online (0 registered, 24 guests)
Index | Admin contact |   Forum Time: May 27, 2020 - 9:03 pm EDT  |  Hits:31,245,221  (6,464 Today )
RSS Feed