XCOPY to backup/rename file

Hello, I need help creating a bat file that will copy a file on a local machine to a network drive.  I have an access database called WORKORDER.MDB right on the C drive of the local machine, I want to copy this access database to H:\Backup\.  I want to maintain the original file name, but time stamp it with date/time.  Example  WORKORDER_11/03/2008 11:34:34 AM.MDB or something close to that.  Can someone help me out?
holemaniaAsked:
Who is Participating?
 
Shift-3Connect With a Mentor Commented:
Paste the script below into a text file with a .cmd extension.  Customize the value of the db variable with the location of the database to be copied.  Running the script will copy and rename the file.


@echo off
setlocal
 
set db=c:\files\workorder.mdb
set dest=h:\backup
 
set stamp=%date:~10,4%%date:~4,2%%date:~7,2%-%time:~0,2%%time:~3,2%%time:~6,2%
 
for /F "tokens=*" %%G in ('dir "%db%" /B /S') do echo F|xcopy "%%G" "%dest%\%%~nG_%stamp%%%~xG"

Open in new window

0
 
BobTheViolentConnect With a Mentor Commented:
Put the first block of code in databasecopy.vbs preferably saved on the computer where the .mdb is located

The second 2 line block goes in a .bat file in the same directory as the vbs file, which calls the vb function passing 2 arguments.  File and directory to copy and what directory/filename to save it as.

Then you could even take it one step further and define a windows "scheduled task" to run the backup routine at intervals like every night at 3am or something.

On Error Resume Next
Dim strCopyFile, strCopyTo, FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
 
strCopyFile = WScript.Arguments.Item(0)
strCopyTo = WScript.Arguments.Item(1)
 
If FSO.FileExists(strCopyFile) AND FSO.FolderExists(strCopyTo) Then
	Dim objCopyFile
	Set objCopyFile = FSO.GetFile(strCopyFile)
	
	  FSO.CopyFile strCopyFile, strCopyTo, True
	
	Set objCopyFile = Nothing
End If
 
Set FSO = Nothing
 
 
 
 
 
 
 
 
 
 
 
echo Copying "filename.mdb" ...
wscript databasecopy.vbs "C:\folder\filename.mdb" "C:\folder\backups\"

Open in new window

0
 
holemaniaAuthor Commented:
Thanks.  Just looking for something like Shift-3 provided.  Just need to create a bat file that will copy the file and put a date with it and save it to a location.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.