Link to home
Start Free TrialLog in
Avatar of BradleyOnTheRoad
BradleyOnTheRoadFlag for United States of America

asked on

BAT file to automate copying files from vpn network to local machine.

Hi, I'm trying to make a BAT file that will automate file management tasks on Windows XP. I would like to do the following:

1)      Check to see if a path exists
   a.      If fail, quit and throw a message box Did not connect
   b.      If success, continue
2)      Copy files from the network resource to the local machine
3)      Test to see if the files were successfully written to local machine
   a.      If fail, message box failed, try again
   b.      If success, message box Copy successful

I guess it will look something like this but I'm sure you'll recognize my VBAish looking if statements:

If { o:\data\files\copyme.mdb path exists } Then
   REM Copy file from O: to C:
   copy /Y o:\data\files\copyme.mdb c:\localdata\copyme.mdb
   copy /Y o:\data\files\copymeToo.mdb c:\localdata\copymeToo.mdb
Else
   Message Box O: Drive not connected
   Exit
End If

If {copy successful} Then
   Message Box Success
Else
   Message Box Copy failed, try again
End if

Exit

I'm a beginner so if I'm going about this the wrong way please let me know. Thanks.
Avatar of Danny Child
Danny Child
Flag of United Kingdom of Great Britain and Northern Ireland image

Here's how to check the path:
https://www.experts-exchange.com/questions/23317031/batch-file-to-read-the-given-path.html

and you can get info to the screen with the ECHO command.
Avatar of BradleyOnTheRoad

ASKER

OK, I took that example and changed it to

@echo off
setlocal
set dr_=C:\FolderDoesNotExist
 
if not exist "%dr_%" (echo Folder "%dr_%" not found goto :EOF)

The BAT file flashes the cmd screen for a second and then goes away. Why isn't there a message to tell me the folder doesn't exist?
Avatar of AmazingTech
AmazingTech

There's no pause. Opening from a command prompt will echo.

Try this.

@echo off
setlocal 
set dr_=C:\FolderDoesNotExist
 
if not exist "%dr_%" (echo Folder "%dr_%" not found & pause & goto :EOF)

Open in new window

You'll need to find some thing the popup the message box.

NET SEND may work for you.
If exist "o:\data\files\copyme.mdb" (
   REM Copy file from O: to C:
   copy /Y o:\data\files\copyme.mdb c:\localdata\copyme.mdb
   If NOT ERRORLEVEL 1 (
       Message Box Successfully copied copyme.mdb
   ) Else (
       Message Box Failed copying copyme.mdb, try again
   )
 
   copy /Y o:\data\files\copymeToo.mdb c:\localdata\copymeToo.mdb
   If NOT ERRORLEVEL 1 (
       Message Box Successfully copied copymeToo.mdb
   ) Else (
       Message Box Failed copying copymeToo.mdb, try again
   )
 
) Else (
   Message Box O: Drive not connected
   GOTO :EOF
)

Open in new window

Try this:
Let me know if you have any questions

save the following as soluition.cmd

:: Author: Robertodf
:: Batch file to copy files from network share or network drive to local folder
:: Filename: solution.cmd
::
:: 1. Check for network folder availability
if not exist \\server\share\file_or_directory goto notexist

:: 2. Copy folder contens to local machine
xcopy \\server\share\folder\*.* c:\localdir /s

:: 3. Test for local folder contents
if not exist c:\localdir\filename.tst goto errorlocal
echo Copy was successful
goto end

:notexist
echo File or Folder not exist
goto end

:errorlocal
echo Local File or Folder not exist, try again
goto end

:end
ASKER CERTIFIED SOLUTION
Avatar of robertodf
robertodf
Flag of El Salvador image

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
Thanks, I see how the conditional statements - as well as the pause - works now.