Order of output in Windows batch files - windows

When I execute the following batch file
#echo off
echo Text from echo
xcopy foobarium
I get the following output:
File not found - foobarium
0 File(s) copied
Text from echo
Obiously the order of the outputs is swapped. How can I ensure that the output appears in the order the commands are specified in?
(This is on Win7)

It seems that this is a bug in clink (a utility that provides bash-like auto-completion for cmd.exe). I've filed a bug report.

Related

Escape # Sign Inside ECHO Command In Batch Script (Called Within VS Post-Build Event)

I simply want to display the # sign inside text that is output by an ECHO command in a CMD batch script that's invoked by a Visual Studio post-build event. How can I do that?
My one-line post-build event invokes my batch script like this:
if $(ConfigurationName) == Release call "$(ProjectDir)\..\MYBATCHSCRIPT.bat"
The file MYBATCHSCRIPT.bat simply states:
#ECHO OFF
ECHO Please create a separate batch file called C:\Temp\OTHERBATCH.bat and place this code inside it:
ECHO #ECHO OFF
ECHO ECHO Hi there!
call C:\Temp\OTHERBATCH.bat
I know there are escape characters (such as ^) that I can use to prefix special characters, but none seem to work for me so far! Help! This is my offending line in the script:
ECHO #ECHO OFF
The intended output for that line is:
#ECHO OFF
All I'm doing is I am providing a hint to the developer to write a separate, prerequisite, non-version-controlled two-line OTHERBATCH.bat batch file. He must write it for my build to work correctly. I am giving him an actual code sample in the build output display.
My code does output the intended result if I run it on a regular command line! It just fails with, "The syntax of the command is incorrect" when I call the batch script in a post-build event! All I just want to do is write "#ECHO OFF" on the screen for my post-build output!
To achieve my desired result (outputting "#ECHO OFF" to the Visual Studio output window, I've had to switch gears due to an unforeseen constraint in how MSBuild processes batch script *.bat files.
I've since learned that the command interpreter used during a Visual Studio post-build event must not be as full-featured as the one behind the CMD prompt that I can launch from Windows--though both run *.bat files. In my limited observation thus far, it will behave differently as follows:
Using apostrophes instead of REM throws an error
Any lines such as these throw an error:
ECHO ECHO OFF
#ECHO #ECHO OFF
The PAUSE command won't wait for the user to hit a key
My solution for the above question was to create a separate text file with the information that I wanted to display on the output window and then use the TYPE command to display it from within my batch script, like so:
TYPE INFO.TXT
Inside INFO.TXT I could place any text that I want to display, including the # sign and text like "#ECHO OFF", without issue.

DOS batch : Different behaviour between command line and drag and drop

I'm trying to write the first argument of a command line in a file, but it works in command line and not with drag and drop.
The very simple batch file (test_echo.cmd) is as following:
#echo OFF
echo %1 > echo_arg_in_file.txt`
On the command line,
C:\rep>test_echo.cmd "C:\rep\fichier de test.txt"`
creates a file echo_arg_in_file.txt with "C:\rep\fichier de test.txt" written inside.
But with a drag and drop of the file "C:\rep\fichier de test.txt" on the batch file, nothing happens... (the test to delete > echo_arg_in_file.txt was done before and displays well "C:\rep\fichier de test.txt")
Any explanation?
I'm not sure about your precise environment, but if I have to bet, current active directory is the problem
Replace your test_echo.cmd with
#echo off
for %%a in (.) do echo %%~fa
pause
Then execute the file both by double clicking it and by drag/drop a file. In both cases you will see the current active directory for the started cmd process.
Why is this relevant? As you have not included a path in the original file redirect, this file will be created in the current active directory that, maybe, could not be what you expect.
You can find more information here
For a quick solution,
#echo OFF
> "%~dp0\echo_arg_in_file.txt" echo %1
that will create the file in the same folder that hold the batch file
What Windows' version. Vista can't drag and drop into a command prompt for security reasons. Restricted possibilities are on later versions (cause we all whinged).
Prior to Vista it was the same as typing the file name if dragged into the window.
If talking about a shortcut each file is one parameter (use shift command to handle this).

How do I store the output to a file AFTER cmd execution?

I have the below code that executes and stores the output to a text file, and then display the output to console.
robocopy %TOBEZIPPED% %TEMPDIR% *.* /E > Log.txt & type Log.txt
but since I'm using the robocopy command that shows progress while it is copying, I would like it to show as it was intended and then store the output (maybe history of the command) to a text file..
How can I do it? I've tried doskey /history from a google search but can't still solve my issue.
Hope someone can help me.. Thanks in advance..
EDIT: I have searched related questions but have not found the same with what I wanted.. please note that the result of output should be displayed first normally (not echoed or typed, see robocopy command) before redirecting it to the output file.. so it's like command will display first as usual, like a command history - after execution, will then be redirected to an output file..
For Shell only:
Use tee:
tee is a command in command-line interpreters (shells) using standard streams which reads standard input and writes it to both standard output and one or more files, effectively duplicating its input.
e.g.
robocopy %TOBEZIPPED% %TEMPDIR% *.* /E | tee -a Log.txt
If you want to do it in windows you need to use PowerShell http://en.wikipedia.org/wiki/Windows_PowerShell . This will provide you tee command to be executable on windows.

Build output from Visual Studio 2010 external tools in output window

I run a batch file as an external tool (by adding it in Tools->External tools) in VS2010 (I've checked the "Use Output Window" option). This batch file performs compilation as in VS command prompt. The project that I'm working on has makefiles for various folders, so I use the mk command to build.
In the batch file, I set up the VS environment and then run the following commands:
cd $directoryWhichContainsFileToBuild
mk
cd main //This directory contains the executable that is to be built
mk
I see the output of the first mk in the Output window but after that it just hangs. I also tried to use an echo after the first mk but even that doesn't get printed in the output window (the ones before it can be seen).
Somewhere I read that there is an issue with VS 2010 output window where it hangs after showing some output, although I couldn't really be sure that that is what's the issue here.
Do I need to enable some other VS setting? Has anybody else encountered this issue?
Thanks.
Update: I unchecked the "Use Output Window" and "Close on exit" option, and I see an extra statement: "Press any key to continue". On doing that however, their is no further processing of the batch file.
Update2: Got it to work by prefixing mk with "call".
Thanks all who tried.
It is always good in batch files to specify executables with full path and file extension instead of just the file name. This avoids often lots of problems.
Here was just mk used instead of mk.bat. Therefore on every compile the command line processor cmd.exe searches for mk.* and then checks if any of the found files have an extension listed in environment variable PATHEXT. The order of file extensions separated by a semicolon in PATHEXT defines the order of execution in case of a directory contains multiple mk.* files.
If a command being specified in a batch file not being an internal command of cmd.exe without path, command line processor searches first for a file with given name in current working directory. This is often one more cause of error. What is the current working directory on execution of the batch file?
Next if no file to execute can be found in current working directory, the command line processor searches in all folders being listed in environment variable PATH separated by semicolons.
So specifying in batch files edited only rarely an external application or another batch file with full path, file name and file extension, in double quotes if necessary because of 1 or more spaces in path or file name, helps command line processor to more quickly execute that application or batch file and avoids problems because of executable not found (unknown command).
Sure, when typing commands in a command prompt window, nobody wants to enter the executables with full path, name and extension. But for batch files it is always good to be not lazy and type files to be executed with full path and extension.
TripeHound has given already the explanation why the observed behavior occurred here.
If a batch file is executed from another batch file without using command call, the command line processor continues batch execution in the other batch file and does never come back. In a C/C++ program this is like using goto with the difference that parameters can be passed to the batch file containing the further commands to be executed next.
But running from within a batch file another batch file with call results in continuation of execution below the line calling the other batch file once the other batch file reaches end, except command exit is used in the called batch file without parameter /B.
So the solution here is using:
cd /D "Directory\Which\Contains\File\To\Build"
call "Path\Containing\Batch\File\To\Build\mk.bat"
rem With mk.bat not changing current working directory change working
rem directory to the directory containing the executable to be built.
cd main
call "Path\Containing\Batch\File\To\Build\mk.bat"
BTW: exit exits command processor, exit /B exits just current batch file. I'll give you three guesses why the parameter is B and not a different letter. Yes, B is the first letter of batch.
Writing as a separate answer instead of an update in the question itself as many readers see the header and skim to the answer: got it to work by prefixing mk with "call". (#TripleHound has also posted the conceptual reason for it in the comment above.)

How do I Pipe Standard Error to a File in DOS (Batch File)?

How do I pipe standard error to a file in a DOS batch file? Piping using >> only pipes the standard output and the standard error still goes to the console.
Details of my issue:
I am running WinRAR via command line, in an automated daily backup. And the following example pipes WinRar's output, but not the error output which is what I want most, to winraroutput.txt:
RAR.exe a -esh -r "E:\backup.rar" "D:\*.*" >> winraroutput.txt
The issue is sometimes files are in use and when they are I want to know they were missed in the archive and record this in a .txt file next to each .rar file in case we ever have to go back. The missing files are easily replaced by reinstalling programs so it's no big deal to replace them, as long as we know they are missing. So it's just information that would be great to know, not necessary, in the time of need.
How do I output just the standard error output to the .txt file and, if possible but not necessary, leave the the regular output to the console?
Bonus points:
Bonus points if you can tell me how to delete the file if it's blank (no errors)! Asked here: How do I Detect (and Delete) a File if it is Empty using a Windows Batch File?.
Try this:
command.exe 2>file.txt
Or if you prefer not to see any errors, send it to nul:
command.exe 2>nul
This should leave std::cout on the console.

Resources