Command operators

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
crobertson
Posts: 20
Joined: 25 May 2012 12:34

Command operators

#1 Post by crobertson » 25 May 2012 12:55

I have a few basic questions that I don't understand what they do.
In this example;
for /f "tokens=* delims= " %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"')

I understand the for /f "tikens=* delims=" but what does the 'dir do exactly, or is that the outter quotes?

What is the ^| do? and
I see find /n /v is just swithces but what with the 3 slashes? "\\\"

Is there someone I can ask these questions directly or is using the form the best method. I've done weeks of research in some of these little operators and still don't understand them.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Command operators

#2 Post by Fawers » 25 May 2012 15:37

crobertson wrote:I have a few basic questions that I don't understand what they do.
In this example;
for /f "tokens=* delims= " %%a in ('dir/b "C:\Documents and Settings" ^| find /n /v "\\\"')

I understand the for /f "tikens=* delims=" but what does the 'dir do exactly, or is that the outter quotes?

What is the ^| do? and
I see find /n /v is just swithces but what with the 3 slashes? "\\\"

Is there someone I can ask these questions directly or is using the form the best method. I've done weeks of research in some of these little operators and still don't understand them.


dir /b "C:\Documents and Settings" will list all visible files and folder in \Documents and Settings. The /b switch stands for "bare": "bare listing", i.e., only file/folder names.

^| is the pipe operator. In this case it is escaped (with ^) because it'll be parsed by the FOR loop; outside the loop, you could write the code in the same way without escaping the pipe:

Code: Select all

dir/b "C:\Documents and Settings"|find /n /v "\\\"

What does it do?
For instance, say we have command1 and command2. You need the output of command1 to be the input of command2.
One way of doing this is,

Code: Select all

command1 >tmpfile.tmp
command2 <tmpfile.tmp
del tmpfile.tmp

But why would you use these 3 lines of code if you could just use 1?

Code: Select all

command1|command2

Example:
say you have CHOICE on your machine.

Code: Select all

echo Y|choice /c:YN /n "Can you see this? "
if %errorlevel% == 1 echo You can see this!


The output of echo Y (i.e. Y) would be the input of choice.
It's just like you've actually typed Y in CHOICE prompt, but you "did" it before the prompt even showd.

The 3 back slashes \\\ is what FIND is NOT trying to find in DIR.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Command operators

#3 Post by foxidrive » 25 May 2012 20:52

To process only directories you would normally add /a:d to the DIR command.

That loop is numbering every item in the DIR /b command presumably for use in the following routine. The "\\\" is used because it will never appear in the filenames or folders so it "numbers every item not having \\\ in it"

It could have simply been find /n /v "" and it would do the same.


This is ambiguous - you normally have "tokens=*" or "delims=" to use the entire line. They are not the same as Tokens=* does not always take the entire line, but filters out some leading characters in some cases.

"tokens=* delims= " are conflicting commands but tokens=* takes precedence there I think.

crobertson
Posts: 20
Joined: 25 May 2012 12:34

Re: Command operators

#4 Post by crobertson » 26 May 2012 14:31

is there a site or book that details all this?

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: Command operators

#5 Post by Ocalabob » 26 May 2012 20:14

Greetings crobertson,

You are at the best site for batch file questions/problems
as demonstrated by the explanations supplied by Fawers and
foxidrive (well done guys!)

I do not know of any book but you may want to have a look
here:

http://www.netikka.net/tsneti/info/tscmd.php

Otherwise, post back with more questions. The folks here are
like 'bums on a ham sandwich' when it comes to batch file
solutions and explanations.

Best wishes!

I forgot this is an international forum. In the USA a bum is a lazy person that would jump on a free meal.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Command operators

#6 Post by Ed Dyreen » 27 May 2012 01:49

crobertson wrote:is there a site or book that details all this?
'
Of course there are, when I was young every OS you bought would come with a book,
I have a book called Tulip MS-DOS 5.0, it used to be new but I used it so much, it is falling apart.
It got me hooked on computers, in those days you only had to own a pc in order to be considered a programmer.

These books teach you much more than just writing batch files, they explain how computers work. How memory is addressed and introduce u to other languages like QBasic.

Ask some gray person with a beard and glasses at your local library :)

Post Reply