Link to home
Start Free TrialLog in
Avatar of mcrmg
mcrmg

asked on

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
Avatar of Michael Dyer
Michael Dyer
Flag of United States of America image

You can use this free command line email utility:

http://caspian.dotconf.net/menu/Software/SendEmail/

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.

SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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



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:

http://scripts.dragon-it.co.uk/links/email-from-batch

There are also some example of BLAT batch files there.

Steve
Hey fellow experts.
You may want to look at poster's profile.
Cheers
Do you mean the 300+ questions deleted?
@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.
Avatar of mcrmg
mcrmg

ASKER

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...

Avatar of mcrmg

ASKER

if there an error handler for xcopy?  thanks
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
Avatar of mcrmg

ASKER

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
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
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
@mcrmg:
Thanks for your reply, honnest feedback.

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

I'll be happy to help you.

Cheers,
Rene

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

ASKER

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
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.

http://scripts.dragon-it.co.uk/links/batch-backup-with-rotation-and-emails

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
Or just use a scheduled windows backup frankly and drop the BKF file up onto a network or removable drive.

Steve
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcrmg

ASKER

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

copied notification" -f myemail@email.com -debug





pause



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.  

Read more about it here:  http://support.quickbooks.intuit.com/support/OLBC.aspx
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@mcrmg:
I forgot to mention to you that I'm sorry for my first post. So please accept my apologies.
Avatar of mcrmg

ASKER

@ReneGe

What post? lol

No worries!! thanks
Avatar of mcrmg

ASKER

Sorry for the delay, I had to spend yesterday fixing other issues.  I will try the suggestion today.thx
Avatar of mcrmg

ASKER

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


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.
Avatar of mcrmg

ASKER

it says

File not found - from
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
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
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.
Avatar of mcrmg

ASKER

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.
Avatar of mcrmg

ASKER

actually, it would be good if my boss can keep one week os the back ups..thanks
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.

Avatar of mcrmg

ASKER

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"
Avatar of mcrmg

ASKER

thank you