troubleshooting Question

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

Avatar of BigDavy
BigDavy asked on
Microsoft DOS
13 Comments1 Solution33488 ViewsLast Modified:
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/Programming/Q_20241272.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?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros