How to write standard error in text file using batch command - batch-file

For one file it's showing standard error on console i.e. command prompt. I want to print it on text file.

some_file.exe >> text.file 2>&1
this will print all output to text file.For more info.

You should use
command 2>file
See http://support.microsoft.com/kb/110930

Related

How to create a program to execute shell command in Windows?

I am trying to execute a shell command or batch file in LiveCode, however, for reasons unknown, it is not working. I would like to use another intermediate program to execute the batch file that records the output to a text file and then read that output with LiveCode as a workaround. What is a simple way to create an executable that can process a batch file?
There is not really any relevant code to share other than
put "test.bat" into tCommand
put shell (tCommand) into fld "output"
The following script works in LiveCode 6.7.6:
set the hideConsoleWindows to true
put shell("C:\test.bat")
My bat file contains
#echo off
echo 'test'
pause
and the value returned by shell() is
'test'
Press any key to continue . . .
The last character of the value returned is a linefeed.
Perhaps you should try to reproduce this simple test.

How to delete content from a text file using windows batch script

I have a demo.txt file. I need to delete content in that file using a batch file. Please tell me the command to delete content for demo.txt file.
break>demo.txt
Try this.it will set an empty file on the place of demo.txt. As break is internal command that does nothing it should be pretty fast.Also the break command can produce output only with /? argument so this makes this method pretty robust.
Command Prompt:
break>c:\'file directory'\demo.txt
PowerShell:
Clear-Content c:\'file directory'\demo.txt
type nul > demo.txt
works and also works in JPSoft's TakeCommand TCC.EXE command shell (where "break" will output "BREAK is ON" rather than nothing as it does in Microsoft CMD.EXE)
The general idea is to find a command that outputs Nothing and redirect that to the file using >
This seems most intuitive to me:
copy /y nul demo.txt
NOTE: Unfortunately, like the other methods provided here, this fails if demo.txt is currently in use by another process. In such a case it is sometimes possible to open the file in a text editor and delete all the contents, even though the file is in use. I don't know of a way to do this from the command line.
If the file is used by another application command prompt redirection may fail (as it requires more file access then necessary). In that case you can use powershell:
PS> Set-Content file.txt $null
Note: do not expect that it will allow access to exclusively opened files.

Bat file prints multiple statements

I am invoking bat command in C++.
The command below writes to console "connecting to bootrom: connected . writing 0x001A13".
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit")
but when i execute same command to dump the above string into file like this:
system("D:\abc\abc.exe -u load D:\abc\13oct\agi\agit">>D:\abc\13oct\tempFile.txt");
It appears that the temp file is having multiple instances of connected . writing 0x001A13 in tempFile.txt
Does any body point me an appropriate fix for this.
Thanks in advance!
OK, I hope I understood this well: The same output you get once if the first command appears twice in the temp file.
That's probably because you're using >> for redirection, which doesn't replace the ouput file, but appends to it.
That means, if you execute echo Test >> tempfile.txt twice, it will have two lines reading Test.
If you want to save only the ouput of the last command to the file, use > instead of >>.

Send text to standard input of a Windows console app

I am trying to write a .bat file to automate some shell commands. Most of the commands are easy and I can just put them into the batch file directly, but there is one command which instead of taking command line parameters, expects you to type in the options you want using "the standard input". I'm not exactly sure what that means. Can someone tell me how to do this? The text I would like to be entered is the contents of one of the files in the directory: "options.txt" which I want to concatenate with a variable inside the batch file "$(additionaloptions)".
Make sense?
The usual way to do this in .bat files is to use echo to write a small text file, then redirect the text file to standard in of the command.
#echo foo > bar.txt
#echo if you need multiple lines >> bar.txt
the_cmd < bar.txt
In your specific example it would something like
copy myfile.txt bar.txt
#echo %variable% >> bar.txt
The fact that you are mention $(variable) suggests to me that this is a makefile rather than a batch file. for a makefile, theres a better way.

Stupid Batch File Behavior. Tries to execute comments

I have tried prefixing lines with semicolons, 'REM', etc.. but no matter what when I run my batch file I keep getting "unknown command REM whatever"
"REM test" It is not recognized, and it is windows vista. I simply get "rem" output back to my console.
That's entirely normal behavior. Batch files are simply sequences of commands that are run one after another. So every line will get output to the console as if it were typed there.
H:\>echo rem test > test.cmd
H:\>test
yields the output
H:\>rem test
as if I typed rem test directly to the console.
You can suppress this by either prefixing the line with #:
#rem test
or by including echo off in the batch file:
#echo off
rem test
If I put ":: test" and execute it I get back "Test".
Can't reproduce here.
If I put "; test" it recursively executes itself
A semicolon at the start of the line seemingly gets ignored.
If you're talking about cmd.exe batch files under Windows, you can use:
rem this method or
:: this method.
For bash and a lot of other UNIX-type shells, you use:
# this method.
I'm pretty certain you're not using cmd.exe since that would give you an error like:
'rem' is not recognized as an internal or external command,
operable program or batch file.
rather then:
Unknown command ...
If you are using a UNIX-type shell, the # character is almost certainly what you're after. If you let us know exactly the shell you're using, we can probably help out further.
you probably created an UNICODE file. These files contain 2 bytes header named BOM
which is not shown by any editor but cmd attempts to execute them and fails.
To make sure this is indeed an issue: type any other command at the very beginning
of your file and see it throws the same error - for example #echo test
To fix it, just create a new plain text file and copy content of the original file there.
then remove the original file and replace it by the newly created one.
In my case the problems are line endings. Somehow Maven or the Jenkins pipeline running on a Linux machine changed the line endings from Windows style (CR LF) to Unix style (LF). Changing them back solves the issue for me.

Resources