Understanding usebackq

I need some help in understanding this command:

FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i

I am particularly confused about the 'usebackq' and what it really does. I am also wondering what ('set') means and what can I replace this with.

I would be appreciative of some examples on the usage of the above and an explanation on its usage.

Thank You.
Who is Participating?
Hi ben1211

To answer your question simply:

SET is the command used to display the system and user environment variables, like PATH and USERNAME.

If you provide SET with arguments it will set or unset variables.

The usebackq argument in the FOR command is used in situations where you want FOR to parse strings that contain one or more double quotes. One example would be parsing a CSV formatted string, particularly of the Microsoft variety, which uses double quotes around each field to allow for embedded commas:
These would not work:
for /f "tokens=1-3" %a in ("field1","field2","field3") do echo %a %b %c
for /f "tokens=1-3" %a in (""field1","field2","field3"") do echo %a %b %c
for /f "tokens=1-3" %a in (`"field1","field2","field3"`) do echo %a %b %c
for /f "tokens=1-3" %a in ('"field1","field2","field3"') do echo %a %b %c

This would work:
for /f "usebackq tokens=1-3" %a in ('"field1","field2","field3"') do echo %a %b %c

Hope that helps
Lee W, MVPTechnology and Business Process AdvisorCommented:
Examples of usage can be found by typing:

FOR /?


SET /?

Before I explain further, can you clarify what context you are asking in?  Basically, why do you want to know/what is the script used for?  (Just want to make sure I'm not answering a home work question).

As leew suggested, you can get much more info by typing /? after the command.
Following is more specific answer to your question:

SET is a command to display/set/remove environment variables. Those variables are mostly used in batch files (but there are some more cases where you can use them).
For example, if you type
SET NAME=ben1211
and then you type
you will see the contents of the variable NAME. You can create a batch file that creates a variable, put data in it according to some actions, and use the data later. Actually, the sky is the limit.

If you type SET with no arguments, you will see all the variables and their values.

FOR is used to run a specified command for each file in a set of files, or for each line in a set of lines.

The command you posted shosws only variable names, without the values.

Normally, the FOR command should receive parameters inside a signle-quote(') or double-quote (")
Sometimes, you need to pass a string or command that already has signle or double quote in it.
So, you use the 'usebackq' which means that you pass a string or command inside a backquote.
In your case, your command:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
could be repalced with this (removed the usebackq, and put the 'set' inside single-quote and not backquote):
FOR /F "delims==" %i IN ('set') DO @echo %i

Some batch tutorials and examples:

For a complete list of commands to use in batch files:

Good luck,
Another nice explenation about the FOR command:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.