Missing characters when command echoed to screen

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

Mostly we put "@echo off" as the first line of a BAT file (command
shell script) unless we want to watch it execute, line by line, in
which case we omit it.

However, even then, the text echoed to the screen does not match the
commands in the file!

My "test.bat" has this line, which should list the subdirectories:

for /D %%d in (*) do echo %%d

however when it is executed, the command is displayed on the screen as

for / %d in (*) do echo %d

I am not too concerned with the double %% getting shown as single %,
but what concerns me is the disappearance of the D after the / mark
in the first options.

Can anyone explain this?

The script still seems to work OK (i.e. it is listing directories
rather than plain files) but why isn't it displayed correctly to
the screen?

I'm using Windows XP 5.1 SP3.

- Rich
 
Rich Pasco said:
Mostly we put "@echo off" as the first line of a BAT file (command
shell script) unless we want to watch it execute, line by line, in
which case we omit it.

However, even then, the text echoed to the screen does not match the
commands in the file!

My "test.bat" has this line, which should list the subdirectories:

for /D %%d in (*) do echo %%d

however when it is executed, the command is displayed on the screen as

for / %d in (*) do echo %d

I am not too concerned with the double %% getting shown as single %,

As well you shouldn't. The command "echo/%variable%" is echoed in its
post-processed state, with the variable value substituted for the reference.
Similarly the for command is echoed with the double percent signs singled.
but...
but what concerns me is the disappearance of the D after the / mark
in the first options.

Can anyone explain this?

I can't really explain either the fact that the D disappears or the fact
that this concerns you. ;-)
The script still seems to work OK (i.e. it is listing directories
rather than plain files)

The script doesn't just seem to work - it works. At least it does what FOR/?
says it is supposed to do, and that includes no mention of how the command
itself is to be echoed...
but why isn't it displayed correctly to
the screen?

I tried this with "for /F", and found that the "F" does NOT disappear. I
suspect that there is no specific rationale for this behaviour of the "for
/D" command. This is indeed strange, as one would assume that the echoing is
done before the command itself begins to execute.

There are a number of anomalies in batch scripting. Some are there by
design, and some (like this one) are just plain puzzling. I wouldn't worry
too much over this one, though. I am quite sure that enough work has gone
into ensuring that the commands do what they are supposed to do that they
generally do what they are supposed to do.

I see this being resolved for you in one of three different ways:

- cmd.exe will be "fixed" so that the "D" does not disappear
- the FOR/? documentation will be updated to indicate that when you see "FOR
/ " you will understand that the actual command was "FOR /D ".
- we'll just write this off as another batch quirk and forget about it.

Of these options, I think the last is the only achievable one - and one we
have had lots of practice exercizing ;-)

/Al
 
Rich Pasco said:
Mostly we put "@echo off" as the first line of a BAT file (command
shell script) unless we want to watch it execute, line by line, in
which case we omit it.

However, even then, the text echoed to the screen does not match the
commands in the file!

My "test.bat" has this line, which should list the subdirectories:

for /D %%d in (*) do echo %%d

however when it is executed, the command is displayed on the screen as

for / %d in (*) do echo %d

I am not too concerned with the double %% getting shown as single %,
but what concerns me is the disappearance of the D after the / mark
in the first options.

Can anyone explain this?

The script still seems to work OK (i.e. it is listing directories
rather than plain files) but why isn't it displayed correctly to
the screen?

I'm using Windows XP 5.1 SP3.

- Rich

I can only confirm your observations. In my book, the omission of the "d" is
a bug.

As to the origins, I'd suggest that "%" escapes "%". It was common to use
ECHO %%whatever%% in constructing subsidiary batch files containing
"%whatever%" so "%%" had a specific meaning to the parser, exploited by
metavariables.

What was echoed to the screen appears to be the command to be executed, as
it would be entered from the prompt.

Whether the string echoed is actually the original string, after being
processed by the parser or whether some attempt has been made to reconstruct
an equivalent command-level string from the fully-processed-ready-to-execute
version, well - who knows?

Either way, I believe that it's simply cosmetic.
 
Back
Top