save output of command prompt in a text file using batch program - batch-file

I want to save the output of command prompt in to .txt file using Batch program(.bat).I have written below batch program,it is creating new .txt file but output is not displayed in the text file.
#ECHO OFF
start java -jar Myjarfile.jar >> Output.txt

Related

open notepad ++ with a batch file and get it to return the prompt after running it

#echo off
rem bath file name: NNP.bat
rem created by: mark g
rem this batch file while open notepad++
"C:\Program Files (x86)\Notepad++\notepad++.exe" %1
this is the batch file i have to open notepad ++ from the command line but it wont return the prompt after i run it until i close out the notepad++ program i know theres a way to get it to return while the program is still open.
what i want it to do is open a file in notepad++ and be able to run more commands
the way its written now it will open the program but waits for the program to close to return the prompt

Created Batch file instantly closes without performing the written code

I am fairly new in using/making .bat files.
I'm trying to make an incredible simple one that combines .txt files into a .tmp file, and then renames the .tmp file in to a .txt file.
This is the code:
#echo off
title Combine Text Files
for %f in (*.txt) do type %f >> Combined.tmp & echo. >> Combined.tmp
rename Combined.tmp Combined.txt
pause
The problem I come up with is that when I try to run the .bat file, nothing happens. CMD closes itself immediately.
If I try to run the script on the third line on CMD, it works fine and it creates the .tmp file.
Similarly, if I run the .bat file sans the script on the third line, I am able to see the "Press any key to continue.." just fine.
Am I doing wrong with the for loop when in .bat files?
You need to double the % when using for command from batch file because this is how the for parser works:
#echo off
title Combine Text Files
for %%f in (*.txt) do (
type "%%~ff">>"Combined.tmp"
(echo()>>"Combined.tmp"
)
rename "Combined.tmp" "Combined.txt"
pause
The errors in the FOR syntax are one of the very few occasions that will terminate a bat file execuction.
As type command supports wild cards you can just try with:
type "*.txt">"Combined.tmp"

Print batch command to file but not console

Is there anyway i can print the command being executed and result of the command in batch to a file but not the console.
Examples:
ls C:\Temp
I want to print ls C:\Temp and result of this command to log file but not on console
call :function "Argument 1"
I want to print call :function "Argument 1" and result of this command to log file but not on console
call perl hey.pl
I want to print call perl hey.pl and result of this command to log file but not on console
command > output.txt
Will create an output file "output.txt" (You can also use ".log"). If such a file exists, it will overwrite it.
command >> output.txt
Will also create an output file, but it will append to it (Useful when you're in a loop and want to append the results of all of the iterations).
Create your batch file, e.g. C:\YourDir\YourBatch.cmd, making sure that you have not turned Echo Off.
At your Command prompt run the batch file.
Either by navigating to the holding path:
CD /D "C:\YourDir"
then invoking
"YourBatch.cmd">"Output.log"
or just run it directly with:
"C:\YourDir\YourBatch.cmd">"C:\YourDir\Output.log"
If you intend to append to an existing Output.log change > to >>.

Windows batch file output to another file

I am trying to list all the files in a directory excluding the extension. I would like to output this into a txt file. I have managed to list all the files successfully.
#ECHO OFF
for %%a in ("c:\wamp64\www\blessd\final\*") do ECHO %%~na
This works fine and all the files in the directory are printed to the screen, however when I try output this to a txt file it does not work.
for %%a in ("c:\wamp64\www\blessd\final\*") do ECHO %%~na > test1.txt
You need to redirect the output of the bat file, not of a single output. If your bat file is My.bat, try:
My.bat > this.out

Save multiple command results to txt file

I have given a bat file that I should run on development server which does some updates.
It has multiple commands and I would like to keep the results in txt file.
Should I add after each command >> result.txt or I can add it somewhere in the end of bat file and everything will be written in that?
You have four options:
Run the batch file at the command line like: myBat.bat >> outPut.txt
Script the above in another batch file and run that
Insert the >> after each command ( Note: the >> appends to file, while > overrides existing text output file)
Enclose all relevant lines in parentheses and script >> result.txt once at the end of the script

Resources