Unexpectedly, "The system cannot find the specified drive." in batch file

I have a problem with a batch file (Windows 7 x64). I managed to reduce it to the following (there are two empty comment lines, it doesn't seem to matter if there is an actual comment):

@echo off if /i [%1]==[] ( echo A :: :: echo B ) 

By placing this in a batch file and then running it without parameters, you will get the following output:

 A The system cannot find the drive specified. B 

Removing one of the lines :: or deleting the surrounding if , fixes everything so that you get the expected result:

 A B 

What's going on here? Why is he looking for a disc?

Edit

Thanks for the answers for the distant. So my question really comes down to the following:

What are the rules for determining whether :: means "the beginning of a comment" or "a colon of letters"?

It might be easier to just go back to REM to add comments, so there is no room for ambiguity, but I find the style :: easier to read.

+7
windows batch-file
source share
2 answers

This is an obscene effect in batch code blocks.

In a code block, a label on one line always requires a secondary line with completely different syntax rules (for a secondary line).
SO: goto command not working

The secondary line must be a command or a simple label, but not a bracket, nor a double colon, nor an internal command attached to the bracket.

MC ND explained that : used here as a regular drive letter, it really is! But only in the secondary line, and even you can suppress the error with subst :: C:\ , the line is treated as a label (you cannot run anything).

 subst :: C:\ ( :label ::\windows\system32\calc.exe ) 

And the second line accepts & , even if the line is a label.

Bad samples

 ( :Label at the end fails ) ( :label ( echo new Block fails ) echo ... ) ( :label :: Double colon fails ) ( :label echo( fails also, as now a file named "echo(" is searched for execution ) 

And now working

 ( ::Works :with a "normal" label in the second line ) ( :But this label! & echo This will not echo'd :but & echo You can see this ! ) ( :label echo or a simple command works too ) 

As sometimes :: used as one comment style , this can cause problems inside the blocks if you do not need a secondary line.

Edit: answer the second question

What are the rules for determining whether :: means "the beginning of a comment" or "a colon of letters"?

The first :: always a comment or label, and the next line is always a drive letter colon .

+14
source share

:: is a valid drive. Just like c: or d: but the letter :

So, your lines are trying to move from the current disk to disk ::

Example: try subst :: c:\ and then dir ::\

+3
source share

All Articles