Solved

Batch file version of "on error resume nest"

Posted on 2011-03-24
3
781 Views
Last Modified: 2012-05-11
I have a batch file to copy logs from single servers to a shared loaction. It seems that if any of the files in the source directory are locked or in use the whole batch file command set fails and issues and exit code of 4. Is ther a line to add to tell the commands if there is an error with any of the files to copy to just move on to the next. Batch file is as follows.

@echo off
set T=%time:~0,5%
set dir=%date:/=-% %T::=-%
echo The target folder is "%dir%".
mkdir "\\usmoksc5app55\text\usmoksc5app27\%dir%"
xcopy /L "D:\GCTI\Logs\*.*" >  "\\usmoksc5app55\text\usmoksc5app27\%dir%\%date:/=-% %T::=%.log"

xcopy /s /c /y "D:\GCTI\Logs\ksc_sip_pri\Archive\*.*" "\\usmoksc5app55\logs\usmoksc5app27\%dir%\"
0
Comment
Question by:charlespliler
3 Comments
 
LVL 29

Expert Comment

by:Randy Downs
ID: 35211109
You could put these in your code.
on error goto

Here's a sample - http://newton.freehostia.com/net/batch/batguide.html
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 250 total points
ID: 35211418
Forget that link. It is so 90's. Nowadays we have much more powerful batch options - but nothing like "on error resume next". Because that is the "default", as long as no fatal error occurs.

I needed several goes to understand what you try to achieve. But I wonder if you really want only the top level files of D:\GCTI\Logs logged into the logfile.
Since %dir% is exactly the same as your logfile name, it sounds a bit superfluous, as date and time is in both the folder and the file name.
Nevertheless, constructing the file name is unnecessary, as you have it in %dir% already:
   xcopy /L "D:\GCTI\Logs\*.*" >  "\\usmoksc5app55\text\usmoksc5app27\%dir%\%dir%.log"

Further, I would rather format the time portion in %dir% like that:
   set dir=%date:/=-% %time:~0,2%-%time:~3,2%

Last note: RoboCopy should be preferred over XCopy. Latter is considered "depricated" since Vista. RoboCopy allows for much more robust copies, repeating until success or an error counter is reached, and many options more.
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 35211701
I also use robocopy amd set it for 1 retry in 1 second. This basically just skips files that don't exist.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now