?
Solved

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

Posted on 2011-10-11
16
Medium Priority
?
588 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:mickeyshelley1
15 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36951905
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
 

Author Comment

by:mickeyshelley1
ID: 36952181
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
 

Author Comment

by:mickeyshelley1
ID: 36952212
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 59

Expert Comment

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

~bp
0
 

Author Comment

by:mickeyshelley1
ID: 36957719
It indicated 1 file copied but there was no additional files such as "copy of" it ran and closed.as before.
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36957758
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
 

Author Comment

by:mickeyshelley1
ID: 36957873
Yes a record locking file named GDB2006_Operator.ldb
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36957898
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
 

Author Comment

by:mickeyshelley1
ID: 36958039
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36958061
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
 

Author Comment

by:mickeyshelley1
ID: 36970775
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36971941
Try adding PAUSE after :End, like this, then run again and look for errors.

:End
pause

Open in new window

~bp
0
 
LVL 15

Accepted Solution

by:
Ben Personick (Previously QCubed) earned 2000 total points
ID: 36990962
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37087443
@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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 37235376
Well done Qc.

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

829 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