Link to home
Start Free TrialLog in
Avatar of mlcktmguy
mlcktmguyFlag for United States of America

asked on

Create .Bat fille to copy a file, adding data and time stamp to the name

Trying without success to create a bat file to copy a file creating a backuip version.

 If possible I'd like the copy of the file created with the data and time stamp at the time of creation.

For example: Original File name 'GRBDQDb01_2013.accdb'  Copied file name 'GRBDQDb01_2013_20200221_1100.accdb'

If that isn't possible I'd like the copied file to create anew version rather than overwrite an existing file of the same name.

For example: Original File name 'GRBDQDb01_2013.accdb'  Copied file name 'GRBDQDb01_2013_X.accdb', where X is the number of version that exist.

The file I want to copy is named 'GRBDQDb01_2013.accdb'.

It is in directory 'I:\My Documents\Access_Databases\GRBLaw\GRBDelinq'

This is what I have so far but it doesn't copy the file, let alone stamp it with the data and time or create anew version.

C:
cd I:\My Documents\Access_Databases\GRBLaw\GRBDelinq
copy I:\My Documents\Access_Databases\GRBLaw\GRBDelinq\GRBDQDb01_2013.accdb I:\My Documents\Access_Databases\GRBLaw\GRBDelinq\GRBDQDb01_2013_PreCompress.accdb 

Open in new window

Avatar of Bill Prew
Bill Prew

Here is my typical approach.


@echo off
setlocal

rem Set folder to work in
set BaseDir=I:\My Documents\Access_Databases\GRBLaw\GRBDelinq

rem Get current date in YYYYMMDD_hhmm format
set Stamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Stamp (
        set Stamp=%%A
        set Stamp=!Stamp:~0,8!_!Stamp:~8,4!
    )
)

rem Backup desired file(s)
copy "%BaseDir%\GRBDQDb01_2013.accdb" "%BaseDir%GRBDQDb01_2013_%Stamp%.accdb"

Open in new window


»bp

Avatar of mlcktmguy

ASKER

Thanks Bill
I created a bat file with your code and the black/cmd screen flashes and closes but no copy is made.  I added double quotes around the directory in
set BaseDir=I:\My Documents\Access_Databases\GRBLaw\GRBDelinq
but that had no effect.
Here's my bat file
@echo off
setlocal

rem Set folder to work in
set BaseDir="I:\My Documents\Access_Databases\GRBLaw\GRBDelinq"

rem Get current date in YYYYMMDD_hhmm format
set Stamp=
for /f "tokens=* skip=1" %%A in ('wmic os get LocalDateTime') do (
    if not defined Stamp (
        set Stamp=%%A
        set Stamp=!Stamp:~0,8!_!Stamp:~8,4!
    )
)

rem Backup desired file(s)
copy "%BaseDir%\GRBDQDb01_2013.accdb" "%BaseDir%GRBDQDb01_2013_%Stamp%.accdb"

Open in new window

To test, open a Command Prompt window.


Then run the bat from there. You'll see any error message that shows

NVIT: Thanks for the tip.
I am getting:
'I:\My' is not recognized as an internal or external command,
operable program or batch file.
Remove the double quotes from set again, this causes more trouble than solving anything. Rather, pay attention to properly surround the var with double quotes when using its value, as Bill did. Mixing both leads to strange and unpredictable results.

The original issue is (probably) that there is a backslash missing from the target path:
copy "%BaseDir%\GRBDQDb01_2013.accdb" "%BaseDir%\GRBDQDb01_2013_%Stamp%.accdb"

Open in new window

so Bills code should have created a copy in the parent folder one level up.,
Thank you, removed the double quotes and now getting

 I:\My Documents\Access_Databases\GRBLaw\GRBDelinq\GRBDQDb01_2013.accdb
The filename, directory name, or volume label syntax is incorrect.
        0 file(s) copied.

The file is definitely named:       GRBDQDb01_2013.accdb

and the directory is for sure:      I:\My Documents\Access_Databases\GRBLaw\GRBDelinq

I cut and pasted both of those name directly from Windows Explorer
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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 Bill, that works.
Welcome, glad that was helpful.


»bp