[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1410
  • Last Modified:

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?
2 Solutions
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

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
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

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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now