• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19068
  • Last Modified:

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.
0
ben1211
Asked:
ben1211
  • 2
1 Solution
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
Examples of usage can be found by typing:

FOR /?

and

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).
0
 
ben-gurCommented:
Hello,

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
ECHO %NAME%
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:
http://www.computerhope.com/batch.htm#03

For a complete list of commands to use in batch files:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx

Good luck,
Adam.
0
 
ben-gurCommented:
Another nice explenation about the FOR command:
http://www.windowsitpro.com/WindowsScripting/Article/ArticleID/38418/38418.html

Adam.
0
 
LittleRed1Commented:
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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now