Link to home
Start Free TrialLog in
Avatar of BigDavy
BigDavy

asked on

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 https://www.experts-exchange.com/questions/20241272/Comment-Out-Batch-Files.html)

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?
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think it's important to point out that XP uses cmd.exe, not command.com 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.
Avatar of BigDavy
BigDavy

ASKER

Thanks for the responses.  I've used double-colons in other spots in my script with no problems.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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:
      Embedded
            Embedded
                  Embedded
                        Remark

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.

Wallsy
Avatar of BigDavy

ASKER

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: https://www.experts-exchange.com/questions/21045653/Batch-files-comments-in-multi-line-blocks.html

As for using REM exclusively for comments:

http://www.obrienpc.net/dos/y-rem.html 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--

Dave
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.
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