FOR command gives error, but only when using colons for comments

I am getting the error message "The system cannot find the drive specified." when I use multiple comment lines in the "do" portion of a "for" line in my batch file.  OS is Windows Server 2003 and also happens on Windows XP.

I am using "::" to precede comments, which some sites recommended rather than "rem".  (including

Below is an example:

@echo off
for %%D in (hi) do (
echo Before a comment
::Comment 1
::Comment 2
echo After a comment

Output is:
Before a comment
The system cannot find the drive specified.
After a comment

If I omit Comment 2, it's fine (no error).  If I omit Comment 1, it's fine.  If I use "rem" for my comments, it's fine.

I can use "rem" to avoid the error, but I'd like to find out why it's happening, if anyone knows or has seen this before....  Perhaps it's just an undocumented (?) limitation of using colons for comments?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
Colons are not meant for comments.  They are meant for defining goto points.  If it executes faster as the poster in the EE comment said, I suspect it's only because it's misinterpreting things.  I would use the REMs.  I have never otherwise heard that :: is faster than REM - besides, faster is relative.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lee W, MVPTechnology and Business Process AdvisorCommented:
I think it's important to point out that XP uses cmd.exe, not by default.  The second link simply sites "easier to read" as the reasoning for use ::.  If you are having problems with :: then I suggest it's because it wasn't designed to be a remark character.  It happens to be usable at times, but it would stand to reason to me that using : as a remark character would actually slow things down because the batch file would have to remember what every instance of : said in order to use it to "goto" when appropriate or if not remember, then have to reread the file whenever a goto was encountered.  REMs should be ignored entirely.
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

BigDavyAuthor Commented:
Thanks for the responses.  I've used double-colons in other spots in my script with no problems.
I agree with leew, adding that comments have no place embedded in loops for any language, whether you think they are ignored or not. Use comments to explain blocks of code, or like headers/subheaders.

Stick three comments just before the loop if you want that many lines to make something clear (the next block of lines). For transportability (across platforms and versions) try to stick to standards everyone knows and uses, and avoid gimmicks.

Since colons are for labeling, using for other purposes adds clutter not to mention confusion. The 2nd colon usually identifies the first character as the drive, and nless mistaken here, isn't colon one of the original 32 drives of dos?
Here my two cents.

I'm not a fan of the double colon comments. They appear to mostly work although it's not a documented feature. In the case of the code blocks you can run into problems (as you found out).

You can even replicate your problem by saying:

:: Comment A
:: Comment B

As SunBow inferred the processing for the code block appears to get confused by the 2nd :: comment and thinks that you're referring to some drive. Like if you were to type in a drive letter and colon for an unmapped drive letter. On my machine, if I type w: I get the same error:

"The system cannot find the drive specified."

I've often read where people prefer :: for comments because of the execution speed and have never given it a 2nd thought. Until now, that is.

I created two batch files one with 10,000 REM statements and the other with 10,000 :: statements. The template for the code looked like this:

@echo off
echo Start>time.txt
REM Test
echo End>>time.txt

On my machine the REM statements executed in 4.18 seconds and the :: statements executed in 2.19 seconds. That is almost twice as fast in favor of the :: comments.

But, how many comments do you normally enter in a batch file. I'll comment something that is particularly hairy, but that's about it.

As SunBow stated it is probably better to not add comments inside a code block. The reason I'd give for this recomendation is that if you are doing a loop a large number of times it would probably be better to leave out the extra processing of a comment. Save that for a comment prior to the loop with a code block.

Someone else asked this question and there was a discussion that did look a lot like this thread :)
leew is correct in his first post.
The first colon by definition creates a "label" for CALL or GOTO to be able to jump to.
The word after that is the identification of that specific label - must be unique to function correctly.
The second colon closes the identification and everything after that on the line is ignored by the parser.  

Starting with two colons together creates a null label that is useless.  It has interesting side effects of NOT expanding variables included on that line. It's not a statement like REM, it's a nul label with ignored characters after it.   Also special characters that can cause execution errors when used in REM statements do not cause errors when included on a null label line.  REM is a statement and as such is parsed.  That takes more time than ignoring a text on a null label line, but if that very tiny difference is important to you then you need a better languange than batch commands anyway, so feel free to use REM.  

It's the way ( ) sends all encluded lines through a new instance of the CMD interpreter that causes lines beginning with :: to fail.  
I aggree with SunBow -  if at all possible, you should put remarks before the FOR statement to prevent the parser from re-hashing the REM statement during every time the do ( ) loops around.  If your ( ) loop is SOOO long you MUST use explanations inside, then use REM.  

- I wouldn't want to mention the sideeffect of the first interpreter ignoring a third colon as just another character after the nul label and passing it to the loop with the rest of the line.  The (  ) generated interpreter uses colons 2 and 3 as if they are 1 and 2 in the child process. Code like this has been shown to work, but you will not see it in any of my stuff (i hope):
echo test 1
::: echo this line ignored - you will not see it echoed.
rem  this works just as well but parses again for each loop
echo done

leew and sunbow were first with correct answers, - I only added this in hopes of making the difference more clear.

As for second colon implying a drive letter, that's just a human perception thing.  Drive letters are only concidered as such when used in context of a file name, reguardless of weather first on a line or in the middle, or surrounded by quotes.  This demonstrates the interpreter believing in a drive letter that is not a drive letter at all (test.txt and server named test do not need to exist for this to work) :
for %%K in ( C:test.txt  \\test\test.txt ) do echo %%K is on drive: %%~dK

good luck (\o/)
Well said Steve,  now we all know i type slow and took at least an hour to get that in there,  lol.
Would you believe i was interrupted several times by a herd of wild horses?

would you believe instead only twice by my lovely wife?

Just my two cents:

Does it honestly make that much difference that a REM statement is processed? On an old 386 every cycle counted, but in these days of 3+GHz processors, pretty much anything run in a CMD script runs at blinding speed. Unless it's an:

I don't think it will cause great pain. Just look at SteveGTR's tests - 10,000 REMs took 2 seconds...

If a batch file takes an age to run, it's probably the commands you're calling, not the remarks.

BigDavyAuthor Commented:
Hi, thanks for all the responses and insight....

I had previously searched this site and couldn't find the issue in the PAQs, but after SteveGTR said it had been discussed before, I tried again and finally found it here:

As for using REM exclusively for comments: says that REM can screw up batch files when the commented line contains a pipe (|) or redirection (<, >, >>) symbol, at least in DOS 7.  However I tested their example of:

REM echo. | date

and this gave me no problems on WinXP.

So now I plan to use REM all the time.

I agree that the speed issue is not anything to worry about (at least for my tiny scripts).  I mostly liked the colons because my scripts were easier to read!  ;)

Thanks again--

Thanks                            [Good Fortune]

Concerning readability, add at least some extra blank lines. Spaces (tabs..) are also good if conducive to columns. I like a <CR> immediately before and after a series (group) of lines, whether they are of a loop, or comments, or whatever (set variables?)
Oh, confession here, I do tend to get in habit also of not only tabbing inside of loops, but of repeating some favorite labels, whether they are needed or not, such as ":done" to give that horizontal break for the general theme of the point of the process. It also reduces debugging a little, by half, when I want a quick and clean jump out of the middle of the program, all that's needed is the goto part. Although I think we (some of us) are correct on the code error being about bad syntax for drive letter, I checked and colon was not among the original six symbols used for that.
Matthew RomanowskiCommented:
Hi, it's Matt, the guy who played REM today. Absolutely absurd experience,

He decided to do so as he spelled out above in Post 1.Hi, guy who played REM today. Absolutely,

He decided to do so as he spelled out above in Post 1. As my Uncle said in a post earlier he won the tickets and had decided to give them to me.

however, he never told me about it. So I had been kinda on an allnighter last night and get a phone call earlier I was beinge recruited for some sort of Hacker or may Blac      k Hat Hacker organization. So i played along.  Eventually all my computer monitors started goign crazy and I got all these text boses from the other actors mid moie. I filmed the entire thing from a few webcams I have up. I never once saw a camera guy inside.

Yeah I didn't realize who tha that upstars and  won't hate on it
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.