CMD outputs in the same txt file - batch-file

I have a batch file with multiple commands:
XCOPY C:\File_path C:\Destination
RENAME C:\File_path New_name
DEL C:\File_path
I need them to write the outputs in the same text file. In this way at the end of the process I have txt file with all the procedure and I can check if everything has been done correctly.
I tried with the following command after each single command/step:
command 1> output.txt 2>&1
but it rewrites the file. It does not keep the output of all the steps but just the last one. How can I do it?
Thanks

According to MS you probably should be using the "append" operator - >>.
This is the article:
https://technet.microsoft.com/en-us/library/bb490982.aspx

use append operator. It will append your output to existing file. Instead of overwriting it.
command 1 >> output.txt 2>&1

Inside batch file, command by command
> output.txt 2>&1 xcopy ...
>> output.txt 2>&1 rename ...
>> output.txt 2>&1 del ...
> will overwrite/create the text file and >> will append to it
Or, wrapping commands inside a block
> output.txt 2>&1 (
xcopy ...
rename ...
del ...
)
Or, outside batch file
myBatchFile.cmd > output.txt 2>&1

If you want to append to a .txt file, you should use >> instead of >

Related

log a output of a command?

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.

How to check for string in log file and move file to different folder according to status using batch

I am having a log file.
I need to loop through the entire file in batch script and if the file contains the text 226 Transfer complete I need to do following:
write to log file File complete;
move the file to different directory;
How can I do that with a batch script?
findstr /L /c:"226 Transfer complete" "yourlogfilename" >nul
if not errorlevel 1 (
echo File complete>logfile
move "the file" "destination directory\"
)
Not that hard...
If you're referring to the Windows command line, which you probably are, here's what you can do:
#echo off
type myfile.txt | findstr /C:"226 Transfer Complete"
if errorlevel 0 echo File Complete >> log.txt

Saving the output of a batch file in command prompt

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

batch to delete files that have their paths in a txt file

I need a batch that will delete files from a LAN, all the paths of the files being saved in a txt file. Don't know how the batch will "read" the paths then delete those files with DEL command.
The line that works so far is:
del "path\*.txt" - for deletion of all txt in some folder (path being the actual line, like c:\folder\folder\*.txt), but I need for a lot more paths.
i pushed then the batch with psexec.exe (for the LAN deletion)
I guess it's 2-3 lines of code, but I'm new to batching & scripting, could someone pls help! Thanks in advance
You can use the FOR /F command to process each line in your input file. Here is a SO answer: How do you loop through each line in a text file using a windows batch file?
This just worked for me (the parentheses around the file name are necessary):
for /F "tokens=*" %%A in (myfile.txt) do del "%%A"
myfile.txt looks like this:
a.txt
b.txt
a b c.txt
del /s *.txt
/s means - delete from all subfolders..
or to iterate over directories:
for /d %i in (*.*) do del %i\*.exe
!!!) you should escape % with % if your code is in batch file

#Del fileName (but file is not available, then will it process further or Stop )?

need to delete a file and then process further.
del file.html
echo ^<center^>^<a href="abc.html"^>ABC^</a^>^</center^> >> file.html
......
echo ^<center^>^<a href="xyz.com"^>XYZ^</a^>^</center^> >> file.html
at first time i got this message Could Not Find C:\Users\file.html but processed further to write into the file. I want to know, can it stop processing further at any stage if the file is not found to delete ?
According to this question - Can I have an IF block in DOS batch file? - DOS can do if branching via
if <statement> (
do something
) else (
do something else
)
And then from this MS Support article you can use EXIST and NOT EXIST with files. So my guess would be:
if EXIST file.html del file.html
Just by the bye, if you wanted to do this in bash (via Cygwin on Windows), it is rather easier as bash has more support for this stuff than DOS:
f="file.html";
if [ -e "$f" ] ; then
rm "$f";
fi
You appear to be appending things to the file, line by line. If there is no processing going on in these lines, you can just concatenate them using copy:
copy chunkA.html+chunkB.html+chunkD.html file.html
That offers the significant benefit that you separate the files containing the data from the script that is stitching them together.
You can check if the file exists first, and proceed with your other commands only if it does.
if exist file.html (
del file.html
echo ^<center^>^<a href="abc.html"^>ABC^</a^>^</center^> >> file.html
echo ^<center^>^<a href="xyz.com"^>XYZ^</a^>^</center^> >> file.html
)

Resources