I wanted to capture the contents of DOS command file (.CMD) to a log file.
I can understnd I can capture the individual commands to log file using > or >>.
However I wanted to copy the contents of the complete dos screen into a log file as well.
I have also noticed when I use >> it removes the output message ( 1 file copied) from the dos console and moves in the log file. Is it possibl I can leave the message is displaied in both console and log file.
Any help will be appreciated.
However I wanted to copy the contents of the complete dos screen into a log file as well.
Make sure not to use "echo off" in your script.
it removes the output message ( 1 file copied) from the dos console and moves in the log file
Redirect stderr to stdout before appending:
myscript.bat 2>&1 >> myscript.log
See here a good resource on batch file redirection.
Is it possibl I can leave the message is displaied in both console and log file
You need "tee". Download a good one for Windows from here. Then do this instead of the above.
myscript.bat 2>&1 | tee -a myscript.log
myscript.bat >> myscript.log 2>&1
Related
I have a BAT script, with the last line being the problem
SET program=%1
SET PWD=%cd%
cd "%~dp0"
"%PWD%\%program%" "filename.txt" ^> "%PWD%\Output.txt" 2^>^&1
And this rightly spits out:
"C:\path\program.exe" "filename.txt" > "C:\Path\Output.txt" 2>&1
However, it then says
Cannot open output file >
And continues on with the script, without any file being created. If I copy and paste what is spit out, it run perfectly.
Note: The behavior is reproducible in an elevated CMD as well.
So, how do I get an EXE to run in a batch script, and redict both stdout and stderr to the same file, without getting an access error?
So your program.exe takes an parameter that is a filename.txt and outputs all to Output.txt
Try without the ^ simbols:
"C:\path\program.exe filename.txt" > "C:\Path\Output.txt" 2>&1
And batch cannot ask for permissions. If you are under c:\ you will need to execute the bat file with administration permission.
I want to redirect the statements it puts on the console into a file in windows batch code. I am using the net stop command to stop a service.
When I do this:
net stop SPTimerV4 2>> mylog.log
it appends error text into the file. But it still prints the regular text on the console:
The SharePoint 2010 Timer service is stopping.
The SharePoint 2010 Timer service was stopped successfully.
How can I even redirect this to a file?
Thanks
net stop SPTimerV4 >> mylog.log 2>&1 is what you're looking for.
This works by redirecting all normal output to the log file (the >>) and redirecting error output a copy of the normal output at that moment, which is appending to a file.
Because it redirects to a copy, you can't actually switch them around (eg. 2>&1 1>> mylog.log won't work)
For more information regarding redirection, please look here
I am running a Tomcat instance by clicking on startup.bat as opposed to invoking the batch file from the command prompt. When I do this, a window with output appears which I would like to capture to a file. Can this be done in any other way than from the command prompt?
You can create another bat file [for example start_tomcat.bat] which will call the original bat file that you mentioned above.
The content of that file could be:
start.bat > yourfilename.txt
where "start.bat" is your original file that you start your Tomcat instance
You can redirect any output of any command to a file by using the ">" character.
For example :
echo HelloWorld > myfile.txt
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.
Background
In Windows 7, when a file is downloaded from the internet, some browsers (e.g. IE and Firefox) flag it as coming from the internet. This is apparent in the properties dialog of the file, which will show a message and an "Unblock" button at the bottom of the properties window.
This property is stored as an alternate stream on the NTFS filesystem - specifically, a stream named "Zone.Identifier". So on a blocked file, you can run the command more < file.exe:Zone.Identifier and you get the output:
[ZoneTransfer]
ZoneId=3
You can clear this data with the command echo. > file.exe:Zone.Identifier. This overwrites the above data with simply a blank line, and while the Zone.Identifier stream still exists on the file, the file is no longer "blocked" as confirmed by the properties dialog.
Problem
FAT32 file systems obviously don't have NTFS alternate streams; so, the command echo. > file.exe:Zone.Identifier gives the output:
The filename, directory name, or volume label syntax is incorrect.
This is output to stdout, so adding 2>NUL on the end does not suppress it. Adding 1>NUL to the end DOES suppress it, however it also suppresses the command from doing anything useful; that is, if you run echo. > file.exe:Zone.Identifier 1>NUL, the Zone.Identifier stream remains.
How can I run the command echo. > file.exe:Zone.Identifier successfully on NTFS, and suppress its error output on FAT32?
Command echo. > file.exe:Zone.Identifier 1>NUL causes redirection of echo. to NUL, the first redirection is ignored.
Adding 2>NUL causes redirection of echo's stderr to NUL. The message you are trying to avoid is printed to stderr by failed redirection and not by echo command by itself.
The solution is to devide the command into two phases by using brackets:
(echo. > file.exe:Zone.Identifier) 2>NUL
This will cause echo. to be executed first and its output redirected to alternative file stream. If trying to write to alternative file stream on FAT filesystem failes, then it's output to stderr will be redirected to NUL.