Quotation Marks.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 113
Joined: 12 Aug 2019 13:57

Quotation Marks.

#1 Post by PAB » 29 Jul 2020 11:13

Good afternoon,

Being an old git, I have always had problems with knowing whether quotation marks should be used or not.
When browsing the Internet, there are many bits of code out there, some of which use quotation marks and some that don't to achieve thae SAME thing.
To try and clarify things in my head, I have a small selection of BITS of code that I would be grateful for guidance on please as to whether they are right or wrong and why!
As I said, these are just snipets . . .

Code: Select all

[01] if /i "%userinput%"=="0"  goto Exit_Program
[02] %ComSpec% /c cleanmgr.exe
[03] if not exist %volume% %Drive%:\ (
[04] if /i "%Drive%"=="C" goto
[05] set "CANCEL_chkdsk="
[06] for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\setup.exe" set setup=%%I
[07] PowerShell.exe Dism /get-wiminfo /wimfile:%setupdrv%:\sources\install.wim
[08] if %OS_Index%==""
[09] if defined autorun (for %%I in (D E F G H I J K L M N O P Q R S T U V W X Y Z) do if exist "%%I:\\sources\install.esd" set setupdrv=%%I)
[10] if not defined setupdrv (
[11]  if %userinput% gtr 2 echo.
What does this do . . .

Code: Select all

set "params=%*"
Is there something like a cheat sheet?

I would really appreciate any input please.

Thanks in advance.

Posts: 136
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France

Re: Quotation Marks.

#2 Post by jfl » 29 Jul 2020 14:10

PAB wrote:
29 Jul 2020 11:13
What does this do . . .

Code: Select all

set "params=%*"
%* means "all arguments"; Or more precisely the whole argument line that was passed to the script or function.
So the above command stores all the script or function arguments into variable params

Now, to go more in depth about this, for maximum safety, I recommend that you actually do:

Code: Select all

set ^"params=%*^"
Prefixing these particular quotes with a ^ ensures that they are not counted as being part of a pair of quotes.
This is necessary because, contrary to Unix shells' $*, cmd's %* includes all the quotes that were passed in the argument line.
These pairs of quotes may protect special characters in between, and if so they are absolutely necessary.
If you insert an additional quote before the %*, you change the zones that are quoted and those that are not.

Ex, with the argument line: arg1 "<arg2>"
set "params=%*" becomes set "params=arg1 "<arg2>"" with two quoted strings: "params=arg1 " and "", and a problematic unquoted <arg2> in between.
set ^"params=%*^" becomes set ^"params=arg1 "<arg2>"^" with still just one quoted strings:"<arg2>" protecting the < and > characters.

The outside ^" mark the beginning and end of the variable assignment. This allows appending other things to that line, that won't be part of the params value. Ex, a comment:

Code: Select all

set ^"params=%*^" &:# This copies all arguments into variable params

As for your many snippets, I won't go into each of them.
The important thing is to mentally replace every %variable% or %%I or %N argument with the value that was provided, including the quotes that it may contain.
If the variable already has quotes, then do not add more in the receiving side;
If the variable does not have quotes, and may contain spaces or special characters like <|>, etc, then you must add a pair in the receiving side.

Posts: 113
Joined: 12 Aug 2019 13:57

Re: Quotation Marks.

#3 Post by PAB » 30 Jul 2020 09:38

Wow, THANK YOU jfl for the detailed reply and insight.
I will have to ponder on that for a while and investigate further to get it clear in my mind.

Post Reply