Using redirects inside a batch file - windows

This seems like I'm missing something obvious but I can't get redirects (>) to work in a .bat / .cmd file.
From the command line, this works as expected and sends the output and error streams to the log file:
doxygen doxygen.config 2>&1 > doxygen.log
Putting the exact same line inside a batch file and running it doesn't work however. It looks like it tries to write the entire command to the log file rather than execute it and then loops??
How do I get output redirects to work inside a bat / cmd file? In case it makes a difference, I am using Win7 and have tried from cmd and powershell.

Perhaps the batch script is calling itself. I would suggest you explicitly specify the extension for the executable your script is calling:
doxygen.exe doxygen.config 2>&1 > doxygen.log

Related

run exe with parameters silently from batch file

I want to execute an executable along with it's parameters from a Batch file silently without printing anything on the console from the executable and the executable shouldn't be executed from another console(it shouldn't open another command-prompt to execute). For that I tried with start command as following and couldn't execute it.
start "C:\myApp.exe -mode='a' -inputFilePath='%filePath%'" -i silent
Where %filePath% is a variable and -mode,-inputFilePath are the arguments to my executable.
Please correct me for anything wrong in above statement and help me out in this. Thanks!
From your batch file, just execute the following:
C:\myApp.exe -mode='a' -inputFilePath='%filePath%' >NUL
the >NUL redirects the output to nothing for that command.

Command line command order

Today I wanted to get a list of files in a particular directory. The easiest way to accomplish this is to open a prompt and run a
dir > filelist.txt
I assumed that first the 'dir' command runs and then its output is created. It is THEN redirected to the file.
What is interesting however is that the directory listing inside of 'filelist.txt' includes 'fielist.txt' with zero bytes which was not expected.
Once the command completes 'filelist.txt' actually shows as having 450 bytes (when I run a dir without the redirection, same prompt).
1) Why does 'filelist'txt' show up at all?
2) What is the command prompt doing behind the scenes?
I assumed that first the 'dir' command runs and then its output is created.
Where would the output live in the meantime?
The output is by default displayed on the console, almost immediately after being output by the program.
Now if you redirect the output to a file instead, it is written to that file as it would be written to the console: at the moment the program writes the output.
Even when a command has no output, the file is still created. The console first tries to create the requested output file; if that fails, the requested command won't even be executed.

On Windows, how do you direct console output to file from batch file when invoked by clicking?

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

running a perl script from a windows scheduled task

I have awstats installed on windows 2008 server.
I schedule the Updatestats.bat file to run every day, the task runs fine without error, but the script is not being executed or is throwing an error that I cannot see.
-- If I run the bat file directly from command line then it works fine. --
I have tried various alternatives to the windows scheduler, such as "nncron" and "Freebyte Task Scheduler", nncron had the same issue, but the freebyte app worked, but sadly it does not run as a service so is of no use.
here is the contents of the bat file, all lines look like this.
c:\strawberry\perl\bin\perl.exe D:\AWStats\wwwroot\cgi-bin\awstats.pl config=earlsmere.co.uk -update
anyone got any ideas ?
Your unattended environment is obviously different from you command line. Check if the following are set:
Script's working directory, if it reads anything from it or uses relative paths.
PERL*_LIB environment variables if your script uses any modules.
PATH environment variable, if your script calls any external scripts/binaries.
User that is running scheduled tasks have sufficient rights for everything you want to do.
As a quick workaround you can set them directly in script using chdir function, lib module, and $ENV{PATH} entry.
You also can try to capture standard output and error with following redirections before you start doing anything else:
open(STDOUT, '>>', '/full/path/to/out.log') || die "Error stdout: $!";
open(STDERR, '>>', '/full/path/to/err.log') || die "Error stderr: $!";
Note that you really should use full paths there in case you indeed have working directory set wrong. And make sure target directory/file is writable for anyone.
Looks like the output gets lost in space...
I suggest redirecting the output of the command to a file, like this:
c:\strawberry\perl\bin\perl.exe D:\AWStats\wwwroot\cgi-bin\awstats.pl config=earlsmere.co.uk -update > c:\my_log.txt 2>&1
(courtesy of Anders Lindahl: Redirect stdout and stderr to a single file in DOS)

Windows .bat file doesn't execute its sequence

I created a simple install.bat file into my application folder, to execute its thing on windows.
But it only executes the first line of the .bat file.
Is there something that I need to add so it continues after the first one is done?
copy something somewhere
move something somewhereelse
gem install etc
Above are the type of commands that are in the .bat.
Do I need to anything something inbetween?
Is the first command in your batch file actually a copy command, or is it a command that's running another batch file?
Running a batch file from another by simply using the second batch file;s name will not return to the calling batch file.
If you want one batch file to invoke another and return you have to use the call command.
Are you overwriting a file? If so you'll need to add the /Y to the copy command to supress the prompt that asks if you want to overwrite the file.
Use the /h parameter to get help on the copy command. It will show this usage and some others.
As written above, all three lines will execute. I imagine that the second and third lines are failing. You should capture the output which will explain why those lines failed.

Resources