mlcktmguy
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_1 100.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\GR BDelinq'
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.
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_1
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
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
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\GR BDelinq
but that had no effect.
Here's my bat file
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
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"
To test, open a Command Prompt window.
Then run the bat from there. You'll see any error message that shows
ASKER
NVIT: Thanks for the tip.
I am getting:
'I:\My' is not recognized as an internal or external command,
operable program or batch file.
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:
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"
so Bills code should have created a copy in the parent folder one level up.,
ASKER
Thank you, removed the double quotes and now getting
I:\My Documents\Access_Databases \GRBLaw\GR BDelinq\GR BDQDb01_20 13.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\GR BDelinq
I cut and pasted both of those name directly from Windows Explorer
I:\My Documents\Access_Databases
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
I cut and pasted both of those name directly from Windows Explorer
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Bill, that works.
Welcome, glad that was helpful.
»bp
»bp
Here is my typical approach.
Open in new window
»bp