[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 2206

# xcopy and send email

Hi,

Currently, I am calling a bat file to copy files using xcopy.  Is there a way then it is done, an email can be sent out?  thx
0
mcrmg
• 13
• 10
• 6
• +3
5 Solutions

Commented:
You can use this free command line email utility:

Simply add the email command to the end of your batch file.

If you want to include a list of the files copies, you have to echo the results to a text file and then add that text file as an attachment using the -a switch.

0

Commented:
Search the "MS DOS" zone for "blat" you will find MANY examples of a great command line tool for this.

~bp
0

Commented:
Great Suggestion - Blat is another good command line email program.  You can find it here:  http://www.blat.net/.  They have great examples of the command line.

I've used this to send notifications in batch files before.  Here is a simple command line you can add to a batch file.  First, move to the directory where blat is installed, then send the email.

c:
cd\
cd email
Blat body.txt -to user@email.com -cc user2@email.com -attach c:\email\filelist.txt -subject "Files copied notification" -f sender@email.com -debug

0

IT ConsultancyCommented:
Another alternative is to call a quick VBScript using CDO.  You can then call that from your batch using

cscript.exe //nologo sendmail.vbs

You can grab a working sendmail.vbs from the attachments at the bottom of my  page here:

There are also some example of BLAT batch files there.

Steve
0

Commented:
Hey fellow experts.
You may want to look at poster's profile.
Cheers
0

IT ConsultancyCommented:
Do you mean the 300+ questions deleted?
0

Commented:
@dragon-it:
I felt like helping, but yes, 300+ deleted and has a certain history of, after experts gives ennuf clues, he answers him self without giving points.
0

Author Commented:
EEs,

I joined this site since 2003 and EEs have been helping me out in all kinds of different area, I work for a small firm which means I have to take care a lot of stuff if not all.  A lot of them I was not even heard of before.

With EEs help, it truly make my life easier.  Regarding the deleted questions, if you have time, you will find those "found answers himeself" questions are just a handful.  Most of them is either reposted, posted once without answers....

having said that, I truly appreciate EE's help...

0

Author Commented:
if there an error handler for xcopy?  thanks
0

Here's an example, from something I do everyday using blat--if interested I will give you instructions on how to setup blat [%FileName%.Txt is the log file I create with various xcopy commands and then combine them into one log file)
C:\Utils\eMail\blat.exe "C:\Backup\Logs\%FileName%.Txt" -tf C:\Utils\Email\Email2.txt -s "Nightly Xcopies for Server on %dt%" -uuencode -server smtp.embarqmail.com -u username@domainname -pw password -log "C:\Backup\Logs\Blat.Csv"
If ErrorLevel 1 GoTo eMail_Err
0

Author Commented:
I got to a point that I can send email and attach a file.  But still need help on where xcopy and error handler come into play..thx
0

XCopy P: /s/e/d/i/c/r/k/y >C:\Backup\Steve.Log
If ErrorLevel 1 GoTo
Goto Darlene

:Err_P
Echo There was an ERROR copying Steve Server1 >C:\Backup\Logs\SteveError.Txt
C:\Utils\eMail\blat.exe "C:\Backup\Logs\SteveError.Txt" -t User@domainname.com -s "Error Steve XCopy Data to Server" -mime -server mail.domain.com -u user@domain.com -pw password -log "C:\Backup\Logs\Blat.Csv"
If ErrorLevel 1 Net Send * Error eMailing Steve
GoTo Darlene

The above xcopy command copies data from the user's PC to the server and the log file Steve is created--if there is an error in the xcopy process (ErrorLevel 1) it goes to a the error section and send me an email by blat and then moves onto the next xcopy section
0

Correction--sorry
If ErrorLevel 1 GoTo Err_P and not If ErrorLevel 1 GoTo

XCopy P: /s/e/d/i/c/r/k/y >C:\Backup\Steve.Log
If ErrorLevel 1 GoTo Err_P
Goto Darlene
0

Commented:
@mcrmg:

I'll see what I can help, however, Lionelmm seems to have things covered up.

Cheers,
Rene

0

Commented:
@mcrmg:
Do you need error handeling on the whole xcopy command or on files individually?
0

Author Commented:
again.  thanks.

Here is what happened, out different office has a quickbooks software which was supposed to backup everyday.  One of the company file was corrupted, when I went to the bk folder, it was EMPTY.  (I did not set this job up, luckly)  My boss had to spend \$1500 to fix the file.

What I am planning to do is bk those files from a entire folder using BackupAssist (we have this already and running), on top of it, I want to use XCOPY to serves as a BK of the BK, just in case.

I am not sure what would be the best way to do the err handling.  There are about 40-50 files in that quickbooks folder.  thx
0

IT ConsultancyCommented:
Ouch.  No "previous versions" copies etc. either then I guess?

I would suggest either robocopy or xcopy would be fine then.

Do an xcopy /d /e of the source to destination and it will copy just newer files.  If the filenames change each day then unless you want the old ones too, and you might end up with a mismatched lot then look at using robocopy /MIR .  This makes the dest area the same as the source area, including deleting files.

Both can be checked with errorlevel afterwards, robocopy has quite a few useful error messages.

An example backup script I have here for instance using xcopy with some error checking, rotation etc.

oddly I don't have one filed there for robocopy but sure someone will add you one now... or just look at search here for robocopy /mir

Steve
0

IT ConsultancyCommented:
Or just use a scheduled windows backup frankly and drop the BKF file up onto a network or removable drive.

Steve
0

Commented:
Also, if your accounting software is still running, you will most likely not be able to copy some of it's locked files. Make sure the accounting software is closed.

Me, I use 7zip to backup our accounting files, and don't do any rotation. Had an issue once that needed to go back for 2 years and find an important accounting problem.  It was possible because I keep all the accounting backups.

"C:\Program Files\7-Zip\7z.exe" a -pASTRONGPASSWORD -mx=9 "%Archive%" "%AccountingDataFolder%" >>"%FileLog%"
0

Author Commented:
this is what I have, very buggy.....does it make sense?thx

xcopy C:\temp\from C:\temp\to /sy
IF NOT ERRORLEVEL == 1 GOTO Err_P
GOTO allok

:Err_P
Echo There was an ERROR copying Steve Server1 >C:\temp\SteveError.Txt
C:\Utils\eMail\blat.exe -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -f myemail@email.com -log

"C:\temp\Blat.Csv" -attach "C:\temp\Blat.Csv"

:allok

Blat body1.txt -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -attach "C:\temp\Blat.Csv" -subject "Files

pause

0

Commented:
Quickbooks offers an online backup solution to keep a copy of your data in the "Cloud".  This might work better for your firm than a batch file solution.

0

IT ConsultancyCommented:
Well what you have could be made easier if you wish, without changing what you have there at this stage, just tidied it up and the errorlevel check does not work like normal variable checks.  Have  amended to use && which means do the next statement if the previous one had no error.  || is the opposite - do something if there WAS an error.

I havent checked the syntax of your blat lines but they look about right!

With xcopy I've added /c continues even if you get an error.  /h copies hidden files too if needed.  /d only if newer as suggestions

@echo off
set log=C:\temp\SteveError.Txt
xcopy C:\temp\from C:\temp\to /s /y /c /h /d > %log% 2>&1 && Goto AllOK

Echo %date% %time% There was an ERROR copying Steve Server1 - Error %errorlevel% >>C:\temp\SteveError.Txt
C:\Utils\eMail\blat.exe -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -f myemail@email.com -log "C:\temp\Blat.Csv" -attach "C:\temp\Blat.Csv"

pause
exit /b

:allok
Echo %date% %time% There were no errors copying Steve Server1 >>C:\temp\SteveError.Txt

Blat body1.txt -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -attach "C:\temp\Blat.Csv" -subject "Files copied notification" -f myemail@email.com -debug

pause
exit /b
0

Commented:
@mcrmg:
I forgot to mention to you that I'm sorry for my first post. So please accept my apologies.
0

Author Commented:
@ReneGe

What post? lol

No worries!! thanks
0

Author Commented:
Sorry for the delay, I had to spend yesterday fixing other issues.  I will try the suggestion today.thx
0

Author Commented:
this is the code I have, I think I am pretty happy aboput this.  I havce a question though, in what case the error will be triggered?  I try to change the foldername, but I did not get the email..thx

rem @echo off
set log=C:\temp\SteveError.Txt
xcopy C:\temp\from C:\temp\to /s /y /c /h /d > %log% 2>&1 && Goto AllOK

Echo %date% %time% There was an ERROR copying ACCT - Error %errorlevel% >>C:\temp\SteveError.Txt
C:\Utils\eMail\blat.exe -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -f myemail@email.com -attach

"C:\temp\SteveError.Txt"

pause
exit /b

:allok
Echo %date% %time% There were no errors copying Steve Server1 >>C:\temp\SteveError.Txt

Blat "C:\temp\SteveError.Txt" -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -attach

"C:\temp\SteveError.Txt" -subject "Files copied notification" -f myemail@email.com -debug

pause
exit /b

0

IT ConsultancyCommented:
What was in the log file "SteveError.txt" ?  Does it show the output of xcopy saying error? What about the date/time stamped "There was an error copying ACCT .... etc."?
It should only run the Gotot AllOK if the xcopy does not return an error level.
0

Author Commented:
it says

0 File(s) copied
Thu 10/20/2011  7:05:49.29 There was an ERROR copying ACCT - Error 4

Also, how cna I generate C:\temp\Blat.Csv, do I have to create the file first?  thanks
0

IT ConsultancyCommented:
OK that is right then... it should have continued to run your blat line.  What does the blat line do if you run it manually?

Your xcopy should be "c:\temp\from\*.*" "c:\temp\to"

and again try running the full xcopy line (upto before the first >) manually to check it works OK.

Not sure what you are after with c:\temp\blat.csv ?  I had copied that from your earlier batch file.  If you want it to send the log file from xcopy still point it at the same file as the other blat line.

Are you sending to a gmail address btw?  If not I suspect their server would not accept the mail anyway unless you authenticate with it.  Do you not have internal mail server?

Steve
0

C:\temp\Blat.Csv is created by the email program blat

I don't see where you have the errorllevel set on your xcopy--its removed from what I see

As further suggestion I have a client that uses QuickBooks too and we too have had instances where QB has failed or become corrupt so I have setup a xcopy that copies the qbw and tlg files for each QB company. To do a restore of the QB files those are the most important and not all the files in the directories however I would still make an xcopy of those especially if they use tax forms and letters from within QB.

I use date to make a copy based on date so my user can go back to a file as far back as 60 days--if you want this info let me know.
0

Author Commented:
I ran the full xcopy line (upto before the first >) manually and it is fine.  But if I change the foldername, it says 0 file copied.

I was thinking I should get an error email, but I didnt.

Our email is hosted by google.  So I am using their email server.
0

Author Commented:
actually, it would be good if my boss can keep one week os the back ups..thanks
0

IT ConsultancyCommented:
Do you mean the dest directory?  Is it already there?  It should create it.

From the log it means it got as far as the line before the blat command.

<<C:\temp\Blat.Csv is created by the email program blat>>
Is it, isn't this just the file he is to attach?!

As to the emails please try the BLAT line manually to see if it goes.  If not you need to determine options that do, might need authentication options for the SMTP.

0

Author Commented:
I should have explained this clearly.  (sorry)

I have two folders, FROM and TO

It works perfectly, I received email letting me know files have been copied.  I have twi questions:

1. If I changed FROM to FROM1, there is an error log, but I did not receive email.

2. Blat.Csv is supposed to be generated by blat, right? but I can not find it.

thanks  , sorry for te confusion.
0

Here's a copy -- if you only want a week and have those older than 7 days removed we would have to add a delete section--I do this manually at the end of each month but I know it can be automated.

@Echo On
TITLE Nightly Copy of QuickBooks from Q: to C:\Data\Users\QuickBooks\Daily
Rem By LionelMM,
Rem      Date: 2008-09-26

:Date
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do Set dt=%%l%%j%%k
GoTo QuickBooks

:Central
Copy C:\Data\QuickBooks\Central-QBE.QBW C:\Data\Users\QuickBooks\Daily\Central-QBE-%DT%.QBW
If ErrorLevel 1 GoTo Error
Copy C:\Data\QuickBooks\Central-QBE.QBW.TLG C:\Data\Users\QuickBooks\Daily\Central-QBE.QBW-%DT%.TLG
If ErrorLevel 1 GoTo Error
Echo * SUCCESS Central QuickBooks on %Date% at %Time% (Q to C:\Data\Users\QuickBooks\Daily) >C:\Backup\Logs\Central.q2d

:621Rentals
Copy C:\Data\QuickBooks\"621 Rentals".QBW C:\Data\Users\QuickBooks\Daily\"621 Rentals"-%DT%.QBW
If ErrorLevel 1 GoTo Error
Copy C:\Data\QuickBooks\"621 Rentals.QBW.TLG" C:\Data\Users\QuickBooks\Daily\"621 Rentals-%DT%.QBW.TLG"
If ErrorLevel 1 GoTo Error
Echo * SUCCESS 621 Rentals QuickBooks on %Date% at %Time% (Q to C:\Data\Users\QuickBooks\Daily) >C:\Backup\Logs\621.q2d
GoTo End

:Error
Net Send * Error Copying QB Files
GoTo Exit

:End
Net Send * Success Copying QB Files
GoTo Exit

:Exit
Exit
0

IT ConsultancyCommented:
blat.csv isn't created by blat afaik.  Don't know why it is there.  I assume you are looking at sending the log like I say:

In your batch, you have first blat run from a specific dir, second not.  Is that the issue perhaps:

@echo off
set log="C:\temp\SteveError.Txt"
xcopy C:\temp\from C:\temp\to /s /y /c /h /d > %log% 2>&1 && Goto AllOK

Echo %date% %time% There was an ERROR copying ACCT - Error %errorlevel% >>%log%

echo There was an error.  About to send mail
blat.exe -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -f myemail@email.com -attach "%log%"
if errorlevel 1 echo There was an error emailing - Error %errorlevel%
pause
exit /b

:allok
Echo %date% %time% There were no errors copying Steve Server1 >>%log%
Echo About to send email saying All OK

Blat  -to myemail@email.com -cc myemail@email.com -server aspmx.l.google.com -attach %log% -subject "Files copied notification" -f myemail@email.com -debug

if errorlevel 1 echo There was an error emailing - Error %errorlevel%

pause
exit /b

0

IT ConsultancyCommented:
0

blat.csv is created by blat if that is what you stipulated as the log file for blat but this log file only shows the failures and successes of blat and not of the xcopy or copy or robocopy commands.

blat.exe -log "C:\temp\Blat.Csv"
0

Author Commented:
thank you
0

## Featured Post

• 13
• 10
• 6
• +3
Tackle projects and never again get stuck behind a technical roadblock.