XCOPY to backup/rename file

Posted on 2008-11-03
Last Modified: 2010-04-21
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?
Question by:holemania
    LVL 2

    Assisted Solution

    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

    LVL 38

    Accepted Solution

    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


    Author Closing Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    DOS FTP 9 205
    DOS command won't run from WScript.Shell 8 47
    Windows 7 Pro - Pause Not Pausing In Batch File 24 52
    Convert JPG to PDF file 4 67
    One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (…
    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now