Odd behaviour from FOR command with seemingly harmless space in it. Explanation? - windows

I ran into an "odd" (to me) problem while writing a batch file, and hope someone can explain why it does what it does... I had a batch file that built-up a moderately complex command-line, and I wanted it to echo what it had built to the screen before executing so that I could check that it had been built correctly. There are other ways I could have done this1, but for various reasons I tried something of the form:
for %%a in (echo "") do %%~a complicated-command-line-with-parameters
The thought being that it would run the do part twice: once with %%a set to echo (displaying the command to the console) and once with it set to "": the expectation was that it would actually execute the command (using %%~a would strip the double-quotes and all that would be left is the command itself). However, while the echo worked, the command itself was not executed.
For a minimal replication, run the following from a command-prompt:
C:\>for %a in (echo "") do %~a dir
C:\>echo dir
dir
C:\> dir
As can be seen, it runs the echo command fine, but it seems that the space in front of dir prevents it from being executed.
Note, though, that manually running a command with a leading space is fine:
C:\> dir
Volume in drive C is OS
Volume Serial Number is A8BD-F861
...
and, were it somehow trying to run the command <space>dir, then it (probably) would have complained of a missing command:
C:\>" dir"
'" dir"' is not recognized as an internal or external command,
operable program or batch file.
Question: The space between %~a (which is an empty string) and the command to be run seems to be causing the whole line to be ignored... does anyone know why?
Note: It make no difference whether it is run from the command-prompt (as above) or from within a .BAT file (after changing %a to %%a etc.). Nor does it make any difference whether the command-to-be-run is a built-in command (e.g. dir as above) or a standalone program.
Further evidence that it is the space between %~a and the command that is causing the problem comes from the "fix" that I found:
C:\>for %a in ("echo " "") do %~adir
C:\>echo dir
dir
C:\>dir
Volume in drive C is OS
Volume Serial Number is A8BD-F861
...
By adding the "separating space" to the "echo " string inside the for command, and removing it from the do clause (...do %~adir), the command works as I originally expected (although I dislike not having a space in front of the command).
After skimming the (somewhat daunting) top answer to the question How does the Windows Command Interpreter (CMD.EXE) parse scripts? that SomethingDark helpfully linked to, a cleaner alternative that seems to work is:
C:\>for %a in (echo call) do %~a dir
C:\>echo dir
dir
C:\>call dir
Volume in drive C is OS
Volume Serial Number is A8BD-F861
...
There are probably idiomatic situations where having the extra call might affect something, but for the moment, it seems to work as desired.
1 I could have put #echo on just before the line inside the batch file, but that also causes the prompt (e.g. C:\MyDirectory\SubDir>) to be shown, which I didn't want. The other "standard fallback" is to just duplicate the line and stick echo in front of the first copy, but then it's too easy for them to get out-of-sync!

SomethingDark already pointed to the explanation.
It's the command vs argument token splitting of a line.
In your case the command token is always %~a this will be replaced later, but even when its empty, the parser will not reevaluate the command token.
With your fix, the dir command is always part of the command token.
But when echo is prefixed it can still echo the remaining part.

Related

Comprehensive list - Running code in batch file vs. entering code directly into console

I am looking for a comprehensive list of differences between running the exact same code in the windows cmd windows (entering it manually, line by line) vs. writing a batch file and running that.
I cannot possibly be the fist person to ask this, but neither Google nor multiple stack overflow searches returned what I wanted. I mostly get comparison between .bat & .cmd like this one, or specific questions about a single issue. I know there are other differences, for example i just found out that
set "filename=foo"
set "optional_postfix="
set "filetype=.cpp"
set completename=%filename%%optional_postfix%%filetype%
echo %completename%
PAUSE
behave differently.
I would like to read up on all of the differences since finding out about them after trying something for half an hour that should work and finding out about the difference afterwards is pretty annoying.
If such a list already exists here, please do not downvote me, I actually spent time looking for it and - at least for me - it was not obvious how to find it. Others might have the same issue, so please just mark it as duplicate.
There are numerous differences whether commands are executed in cmd or in batch file context, all of which refer to cmd-internal commands or features though.
The following commands that do nothing when executed in cmd context (they do not produce any output and do not affect ErrorLevel):
goto
shift
setlocal (to en-/disable command extensions or delayed expansion in cmd, invoke the instance using cmd /E:{ON|OFF} /V:{ON|OFF} instead)
endlocal
Labels (like :Label) cannot be used in cmd, they are simply ignored. Therefore call :Label cannot be used too (if you do, an error message appears and ErrorLevel becomes set to 1).
There is of course no argument expansion in cmd since there is no possibility to provide arguments. Hence a string like %1 is just returned as is ( try with echo %1). This allows to define variable names in cmd that begin with a numerical digit, which is not possible in batch files (actually you can define them, but you cannot %-expand them; try this in a batch file: set "123=abc", then set 1; you will get the output 123=abc; but trying to do echo %123% will result in the output 23, because %1 is recognised as argument reference (given that no argument is supplied)).
Undefined environment variables are not expanded (replaced) in cmd but they are maintained literally (do set "VAR=" and echo %VAR%, so you get %VAR%). In batch files however, they are expanded to empty strings instead. Note that in batch files but not in cmd, two consecutive percent signs %% are replaced by a single one %. Also in batch files but not in cmd, a single % sign becomes removed.
More information about the percent expansion (both argument references and environment variables) can be found in this thread: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
The set /A command behaves differently: in cmd it returns the (last) result of an expression on the console (without trailing line-break), but in batch files it does not return anything.
The for command requires its loop variable references to be preceeded by two percent signs (like for %%I in ...) in batch files, but only a single one (like for %I in ...) in cmd. This is a consequence of the different percent expansion handling in batch files and in cmd.
Finally, some commands do not reset the ErrorLevel upon success, unless they are used within a batch file with .cmd extension. These are:
assoc
dpath
ftype
path
prompt
set
More information about resetting the ErrorLevel can be found in this post: Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?

Echo a blank (empty) line to the console from a Windows batch file [duplicate]

This question already has answers here:
How can I echo a newline in a batch file?
(24 answers)
Closed 8 years ago.
When outputting status messages to the console from a Windows batch file, I want to output blank lines to break up the output. How do I do this?
Any of the below three options works for you:
echo[
echo(
echo.
For example:
#echo off
echo There will be a blank line below
echo[
echo Above line is blank
echo(
echo The above line is also blank.
echo.
echo The above line is also blank.
Note: Though my original answer attracted several upvotes, I decided that I could do much better. You can find my original (simplistic and misguided) answer in the edit history.
If Microsoft had the intent of providing a means of outputting a blank line from cmd.exe, Microsoft surely would have documented such a simple operation. It is this omission that motivated me to ask this question.
So, because a means for outputting a blank line from cmd.exe is not documented, arguably one should consider any suggestion for how to accomplish this to be a hack. That means that there is no known method for outputting a blank line from cmd.exe that is guaranteed to work (or work efficiently) in all situations.
With that in mind, here is a discussion of methods that have been recommended for outputting a blank line from cmd.exe. All recommendations are based on variations of the echo command.
echo.
While this will work in many if not most situations, it should be avoided because it is slower than its alternatives and actually can fail (see here, here, and here). Specifically, cmd.exe first searches for a file named echo and tries to start it. If a file named echo happens to exist in the current working directory, echo. will fail with:
'echo.' is not recognized as an internal or external command,
operable program or batch file.
echo:
echo\
At the end of this answer, the author argues that these commands can be slow, for instance if they are executed from a network drive location. A specific reason for the potential slowness is not given. But one can infer that it may have something to do with accessing the file system. (Perhaps because : and \ have special meaning in a Windows file system path?)
However, some may consider these to be safe options since : and \ cannot appear in a file name. For that or another reason, echo: is recommended by SS64.com here.
echo(
echo+
echo,
echo/
echo;
echo=
echo[
echo]
This lengthy discussion includes what I believe to be all of these. Several of these options are recommended in this SO answer as well. Within the cited discussion, this post ends with what appears to be a recommendation for echo( and echo:.
My question at the top of this page does not specify a version of Windows. My experimentation on Windows 10 indicates that all of these produce a blank line, regardless of whether files named echo, echo+, echo,, ..., echo] exist in the current working directory. (Note that my question predates the release of Windows 10. So I concede the possibility that older versions of Windows may behave differently.)
In this answer, #jeb asserts that echo( always works. To me, #jeb's answer implies that other options are less reliable but does not provide any detail as to why that might be. Note that #jeb contributed much valuable content to other references I have cited in this answer.
Conclusion: Do not use echo.. Of the many other options I encountered in the sources I have cited, the support for these two appears most authoritative:
echo(
echo:
But I have not found any strong evidence that the use of either of these will always be trouble-free.
Example Usage:
#echo off
echo Here is the first line.
echo(
echo There is a blank line above this line.
Expected output:
Here is the first line.
There is a blank line above this line.
There is often the tip to use 'echo.'
But that is slow, and it could fail with an error message, as cmd.exe will search first for a file named 'echo' (without extension) and only when the file doesn't exists it outputs an empty line.
You could use echo(. This is approximately 20 times faster, and it works always. The only drawback could be that it looks odd.
More about the different ECHO:/\ variants is at DOS tips: ECHO. FAILS to give text or blank line.

Writing to file with batch

I'm trying to get a batch file to take user input and place it in a file... here is my code so far.
set /p input path=Path:
echo %path% >> log.txt
when I turn echo off, it's putting a "1" infront of the chevrons like so:
echo C:/Example/Path 1>> log.txt
the system can not find the file specified.
Please can anyone explain this
Certainly - I'll answer the question asked.
Originally, >somewhere or any redirection sent the data to the destination specified.
With the changes to the NT version, this was expanded. A digit DIRECTLY preceding a redirector means logical file number where 0=STDIN (standard-input) 1=STDOUT (standard output) and 2=STDERR (Standard error.) The others are undefined. This can cause problems where the data (typically) to be output ends with a digit, hence the >filename echo ... syntax
Consequently, for backward compatibility, (eg) >nul is still processed as it always has been, but it's ECHOed in its explict form 1>nul - sending STDOUT to nul to distinguish it from 2>somewhere_else
try this:
set /p "MyNewPath=Path: "
>>log.txt echo %MyNewPath%
Your SET statement is wrong. :-) Also, PATH is already assigned in the environment (the Windows PATH, and altering it may cause problems with running other commands in your batch file.
Try this instead:
set /p "Input=Path: "
echo %Input% >> log.txt

"was unexpected at this time."

I'm running this command on a batch file:
for %I in (*.txt *.doc) do copy %I c:\test2
...and it keeps returning:
I was unexpected at this time.
What is the cause of this error?
If you're running within a batch/cmd file, you need to double the % markers:
for %%i in (*.txt *.doc) do copy %%i c:\test2
The single % variant only works from the command line.
If being run from a batch file, variables need to be denoted with two percent signs, like %%I, only from the command line you use one
I ran across a case where I was getting this error from a file that was named *.cmd. The error arose when I tried to access the first argument to the batch command:
if %1 EQU ""
Once I put quotes around the symbol for the first argument, the warning message went away:
if "%1" EQU ""
Not a direct answer to the question, but if you encounter this message in any program, batch command, etc. then it's most likely related to your PATH containing " characters.
For instance, in Atom editor I was getting the message in the settings view.
"\"GNU was unexpected at this time
This was due to a different program putting in my PATH the following entry
...;C:\"Program Files"\"GNU ARM Embedded;..."
Because of that, the antislash character is read as escaped by some programs, which causes issues because then it's not a pathname delimiter but a simple character.
The solution for me was to remove those " from the PATH, and everything worked fine.
...;C:\Program Files\GNU ARM Embedded;...
PS: I have a doubt whether or not this can impact the original program (GNU ARM Embedded in this case) that maybe does not support spaces in pathnames. If somebody with more insight can clarify in the comments, I will update my post.
Hope this helps

Is there any way to trace through the execution of a batch file?

I inherited some large batch files, and I'd like to rewrite them to a more "developer friendly" language.
I'd like to find out the following things:
what other scripts it calls
what other processes it starts
what files does it write to
what environment variables it uses, which ones does it set
For the last point, I'm aware I can do this before I start:
set > original_environment.txt
And after I run it, I can do this:
set > new_environment.txt
and just do a diff between them ... but I'll probably miss some variables that may be unset when the script finishes ( or even all of them if the script's ran under setlocal ).
Is there any way of finding all those things without me adding tons of echo statements throughout the script code?
Is there such a tool that can monitor the process started by the batch file and tell me everything it did?
You can just look inside them and figure out what they do.
You can also remove any echo off statements and # preceding commands; that way every command is output before it's run and you can redirect the output to a file to study it later.
There is no debugging tool for batch files that I am aware of—but I contemplated writing one once.
There is no direct way to do it. But it's not impossible to create one.
Since Windows XP/Vista/7 came out the good'ole set of DOS batch commands has been greatly upgraded, although not many uses them or even RTFM (FOR /??)
So here I give you, a simple pure-batch TRACER that utilizes the FOR /F line-parsing switch:
#ECHO OFF
FOR /F "delims=" %%L IN (%1) DO (
CLS
ECHO __________________________________________________
ECHO ENV. VARIABLES *BEFORE*
SET
ECHO __________________________________________________
ECHO LINE
ECHO %%L
ECHO __________________________________________________
ECHO Hit any key to execute the line ...
PAUSE > NUL
ECHO __________________________________________________
ECHO EXECUTE
%%L
ECHO __________________________________________________
ECHO Hit any key to fetch the next line...
PAUSE > NUL
)
ECHO END OF FILE
You can take it as a start and modify it as you go.
Here's how you'd use it:
DEBUG.BAT TEST.BAT
And I'll also give you a test file to try it out:
#ECHO OFF
ECHO Hello World!
SET aaa=1
SET bbb=2
ECHO Doing step 2
SET aaa=
SET ccc=3
ECHO Doing step 3
SET bbb=
SET ccc=
ECHO Finished!
This DEBUG.BAT thing, however, due of its simplicity, has some limitations BUT which can be worked around if you slap enough BATCH-fu in there.
It cannot process multi-line blocks :: This can be worked around by having the FOR commands parse tokens and build the lines as they come in, and IF it encountered an open parenthesis, just dump the parenthesis block content to a temporary file and then call itself on the tempfile e.g. DEBUG tempfile.bat
It cannot process jumps :: You can of course, do an IF check for a GOTO label then do a FOR /F to parse out label itself, then maybe utilize the second argument %2of DEBUG.BAT to specify the label to jump to, in which case if this very argument is specified you'd just spin the FOR /F until the desired label came into view and then proceeds with normal debugging on the next line.
There is too much information from a single SET :: Just do what you did with the SET > before.txt and after thing but do it on each line and then run a cmd-line DIFF tools on the files (plenty are available on the net). Then you'll get a DIFF of each variable that has changed since last step. You might even be able to avoid the env. variables mess altogether by slapping in SETLOCAL and ENDLOCAL in there and then you'd get just the local SETs ... but YMMV.
Those are some. If you've found some show-stopping limitation or whatever enhancements would help you nail that last bug, feel free to just let me know (via the comments) and I'll try to help you if I can.
Hope this helps.
No, there is no way to debug old style batch files. You can, however, just get rid of all the ECHO OFF statements, and then all the commands and output will be echo'd to the console when you run them.
If you are willing to spend some money you should take a look at the Running Steps batch file IDE and its debugging capabilities.
I didn't test it, but it has some features that might help you in your task:
...
Visual Studio-like debugging environment.
Rich set of debugging commands (step into, step over, step out, and more)
Rich Project analyzer to find your errors and warnings in no time.
Integrated support for delayed-expanded environment
variables.
Multi-type breakpoint definitions to fit your multiple debugging needs.
Complex pipeline and redirection support with multi-color highlighting.
Environment variable visualization and modification support.
Expanded information window for true variable definition visualization.
Impressive 'For command' unrolling feature.
Interactive callstack and Parameters window.
...
They also offer a trial version.

Resources