Modify .Bat File to Notify User If Unable To Copy File

I need to modify the following .bat file to notify user if a destination file is open and it is unable to copy and replace the that file.
echo off
CLS
:menu
echo.
echo .....................................................................
echo PRESS 1 or 2 for selection.
echo .....................................................................
echo.
echo 1 - Copy MDE file from Server to Workstations
echo 2 - EXIT
echo.
set /p M=Type 1 or 2 then press ENTER 
IF %M%==1 Goto Update
IF %m%==2 Goto End
:Update
xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\station1\c$\generald\front /r /y /f
xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\station2\c$\generald\front /r /y /f
xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\station3\c$\generald\front /r /y /f
xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\station4\c$\generald\front /r /y /f
:End

Open in new window

mickeyshelley1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
Pretty easy to check for ANY errors from the XCOPY command, could do something like this, it will check for any errors after each run of the XCOPY command, and call the DoErrors subroutine to process.  Alert as needed there.

echo off
CLS

:menu
  echo.
  echo .....................................................................
  echo PRESS 1 or 2 for selection.
  echo .....................................................................
  echo.
  echo 1 - Copy MDE file from Server to Workstations
  echo 2 - EXIT
  echo.
  set /p M=Type 1 or 2 then press ENTER 
  IF %M%==1 Goto Update
  IF %m%==2 Goto End
  goto :End

:Update
  for %%A in (station1 station2 station3 station4) do (
    xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\%%A\c$\generald\front /r /y /f || call :DoErrors %%A
  )
  goto :End

:DoErrors
  echo Errors when copying "%1"
  goto :EOF

:End

Open in new window

~bp
0
mickeyshelley1Author Commented:
I ran this with an open file on station2 and received no indication that there was a problem, how will it react to an open file?
0
mickeyshelley1Author Commented:
Would it be possible for write to a text file if the xcopy was completed or if it was incomplete because of an open file
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Bill PrewCommented:
Did xcopy display any error to the screen when the file was open, or does it just go ahead and copy anyway?

~bp
0
mickeyshelley1Author Commented:
It indicated 1 file copied but there was no additional files such as "copy of" it ran and closed.as before.
0
Bill PrewCommented:
That seems to indicate that xcopy doesn't realize the file is open then, and is able to copy over it.

Given that, we would need something else to determine if the file is in use.  Are there any additional files that are created where the MDE file exists, that could be checked as a way of knowing the MDE is in use?

Or any other way to know?

~bp
0
mickeyshelley1Author Commented:
Yes a record locking file named GDB2006_Operator.ldb
0
Bill PrewCommented:
Okay, give this a try.

echo off
CLS

:menu
  echo.
  echo .....................................................................
  echo PRESS 1 or 2 for selection.
  echo .....................................................................
  echo.
  echo 1 - Copy MDE file from Server to Workstations
  echo 2 - EXIT
  echo.
  set /p M=Type 1 or 2 then press ENTER 
  IF %M%==1 Goto Update
  IF %m%==2 Goto End
  goto :End

:Update
  for %%A in (station1 station2 station3 station4) do (
    if exist \\%%A\c$\generald\front\gdb2006_operator.ldb (
      call :DoErrors %%A
    ) else (
      xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\%%A\c$\generald\front /r /y /f
    )
  )
  goto :End

:DoErrors
  echo Errors when copying "%1"
  goto :EOF

:End

Open in new window

~bp
0
mickeyshelley1Author Commented:
The code ran and then errored out when it came to an open file and closed immediately.

Ideally, when the code runs it would continue to the next xcopy after error and leave some form of notification on which xcopy failed such as a msgbox or log in text file located in the same directory as code
0
Bill PrewCommented:
The code I provided should keep going, did you modify it at all?

It will execute the subroutine :DoErrors, and then return to the looping off the files to copy.  Did you see the error "Errors when copying ..." that it should have displayed?  Or did it produce some other error?

~bp
0
mickeyshelley1Author Commented:
Station2 was the open file I used for testing, it copied to station1 and indicated 1 file copied as it begain to copy to station2 it indicated error coping file and closed before i could read it further. I didn't continue to copy to the other location. It takes aprox 5 seconds to copy to each location. I copied and pasted it as it is below i didn't make any intentional changes..





.
echo off
CLS

:menu
  echo.
  echo .....................................................................
  echo PRESS 1 or 2 for selection.
  echo .....................................................................
  echo.
  echo 1 - Copy MDE file from Server to Workstations
  echo 2 - EXIT
  echo.
  set /p M=Type 1 or 2 then press ENTER 
  IF %M%==1 Goto Update
  IF %m%==2 Goto End
  goto :End

:Update
  for %%A in (station1 station2 station3 station4) do (
    if exist \\%%A\c$\generald\front\gdb2006_operator.ldb (
      call :DoErrors %%A
    ) else (
      xcopy \\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde \\%%A\c$\generald\front /r /y /f
    )
  )
  goto :End

:DoErrors
  echo Errors when copying "%1"
  goto :EOF

:End

Open in new window

0
Bill PrewCommented:
Try adding PAUSE after :End, like this, then run again and look for errors.

:End
pause

Open in new window

~bp
0
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
I realize now that Billprew's code is very similar to the code I just wrote, but I already took the time to write it, so, I'm sticking it up.

The differences are minor besides how he and I format and how we check file existence, however my code will pop-up a Windows Message box whenever there is an error as well as output that error to the screen.

Also, in my script it will not accidentally run through the remainder of the script if the wrong option is pressed in the menu.

Finally, and I think Bill is very much aware of this, there's no need to do a Call to the errors label unless we're looking to do something more complex, so mine takes care of the messages in-line.

  I'm guessing Bill is expecting you'll be doing more things if the copy fails, in which case his makes more sense and I would suggest you use that instead.

Another very real thought is that you may want to capture the output of the XCopy command to display in your error, this is doable in either script by adding another for loop into it.  But would be an easier addition using the call function as delayed expansion would not need to be used.

-Q
::Script Name: CopyMDE.bat
:Begin-Script
	@ECHO OFF
	:Start-Setup
		SET "M=0"
		SET "SrcFilePath=\\odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde"
		SET "DstFilePath=c$\generald\front"
		SET "DstCmpList=Station1,Station 2,Statio n3,Sta tion4"
		
	:End-Setup

	:Start-Menu
		CLS
		echo.
		echo .....................................................................
		echo PRESS 1 or 2 for selection.
		echo .....................................................................
		echo.
		echo 1 - Copy MDE file from Server to Workstations
		echo 2 - EXIT
		echo.
		IF %M% NEQ 0 ECHO You need to pick 1, or 2, no other selections are accepted!&ECHO.
		set /p M=Type 1 or 2 then press ENTER 
		IF %M%==1 Goto Start-Copy
		IF %M%==2 Goto End-Script
		GOTO :Start-Menu
	:End-Menu

	:Start-Copy
		SET "DstCmpList=%DstCmpList:,=", "%"
		FOR %%A IN ("%DstCmpList%") DO (
			IF NOT EXIST "\\%%~A\%DstFilePath%\gdb2006_operator.ldb" (
				XCopy "%SrcFilePath%" "\\%%~A\%DstFilePath%" /r /y /f||ECHO Error! XCopy "%SrcFilePath%" "\\%%~A\%DstFilePath%" /r /y /f Failed!&msg %UserName% "Error! "\\%%~A\%DstFilePath%\gdb2006_operator.ldb" Failed!"
			) ELSE (
				ECHO Error! "\\%%~A\%DstFilePath%\gdb2006_operator.ldb" Exists!&msg %UserName% Error! "\\%%~A\%DstFilePath%\gdb2006_operator.ldb" Exists!
			)
		)
	:End-Copy

:End-Script
ECHO.
ECHO Script has exited at %TIME% on %DATE%!
PAUSE

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Paul TomasiCommented:
@echo off
setlocal enabledelayedexpansion

set source=odc-sql\generald\pgram\sqlfront\gdb2006_operator.mde
set destination=c$\generald\front
set errorfile=errors.txt

:menu
cls
echo.
echo .....................................................................
echo PRESS 1 or 2 for selection.
echo .....................................................................
echo.
echo 1 - Copy MDE file from Server to Workstations
echo 2 - EXIT
echo.
set /p M=Type 1 or 2 then press ENTER 
IF %M%==1 Goto Update
IF %m%==2 Goto End
goto :End

:Update
echo %date% %time%>>%errorfile%
for %%a in (station1 station2 station3 station4) do (
  xcopy \\%source% \\%%a\%destination% /c /f /r /y 2>>%errorfile%
  if !errorlevel! neq 0 echo Update failed on: %%a>>%errorfile%
)

notepad %errorfile%

:End

Open in new window

0
Bill PrewCommented:
Well done Qc.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.