I wanted to make a logging function for my batch app and what I want this function to do is save the output of a command. As an example lets take a simple command such as
copy "data" "C:\Program Files"
where in the folder data is "text.txt" & "text2.txt". The output of the command would be
C:\users\%username%\desktop\testFolder\data\text.txt -> C:\Program Files\data\
C:\users\%username%\desktop\testFolder\data\text2.txt -> C:\Program Files\data\
2 file(s) copied
I want this to be saved into lets say "output.log" which would be in the same folder as the folder "data" which was copied. is there a way to do this?
> character is what you need.
command > log.txt
Creates and overwrite content of log.txt with command result.
command >> log.txt
Creates or append content of log.txt with commandresult.
command >> log.txt 2>&1
Creates or append content of log.txt with command result and error.
Related
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 >>.
My scheduler will run every 15 mins and I don’t want to copy the same file again.
I have the script below in a batch file running in the task scheduler:
cd C:\Program Files (x86)\WinSCP
FOR /R C:\Utils\TRASACTION\ICP %%f IN (*.LOG) DO TYPE NUL>%%f
Winscp.com /script=C:\Utils\TRASACTION\ICPS\download.txt >> C:\Utils\ICPS\lowe.log
set CURRENT_DATE=%date:~10,4%%date:~4,2%%date:~7,2%
xcopy C:\Utils\TRASACTION\Accounting_file\%CURRENT_DATE%\ACCOUNTING_FILE_* Y:\Transactions\In >> C:\Utils\TRASACTION\ICPS\lowe.log
exit
in line 3 that download.txt contains the below script
option echo off
option batch on
option confirm off
open sftp://icps:xxxxxxxxx#11.111.111.111
lcd "C:\Utils\ TRASACTION\ICPS"
cd /Out/1.ACCOUNTINGFILE
get %TIMESTAMP#yyyymmdd% C:\Utils\ TRASACTION\Accounting_file\
exit
I would like to insert a check in the script when the folder is available then proceed to the copy or else exit.
Or is there another way not the copy the file from the folder that has been already copied to the directory in below line?
(xcopy C:\Utils\TRASACTION\Accounting_file\%CURRENT_DATE%\ACCOUNTING_FILE_* Y:\Transactions\In >> C:\Utils\TRASACTION\ICPS\lowe.log
Thanks for your help.
When joining multiple files with
type *.xml >> temp.txt
The command line (or batch script) would print each file name found on the screen. Is there a workaround to make it silent?
Since I'm adding an extra code on the first line of the temp.txt file, I can't use copy *.xml temp.txt.
Is there anyway to write the command windows output to a file? To have a transcript if you will.
Yes, for example, on the command prompt you can do this:
dir *.* > c:\temp\a.txt
This operation is called 'output redirection'. The output of the above command will go the named file and will overwrite it if it already exists with the results. You can find many examples on the net for that, example: Redirection.
In command prompt, use >> operator.
For example, If you type below command then output will be saved to output.txt file
dir > output.txt
If you type below then output will be appended to exist file.
dir >> output.txt
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