batch file to copy file, appending day of month

Hi!

I'm using this batch file to backup my database, appending the day of the month to the end of the file name (so that I will always have a month's worth of backups).

However, when the batch file runs, it copies the file without appending. Anyone know why that would be?

set DATE=%date% <- extracts date from system date command
set DAY=%DATE:~7,2% <- extracts two characters starting at 8th (0-7)

COPY "F:\AccessDatabase\Merchandising.accdb" C:\Users\Me\Desktop

\Merchandising_%DAY%.accdb"
pause

Open in new window

LVL 10
etech0Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wshark83Commented:
i think you are missing an additional " mark should be something like this:

also a good things to add /b and /v switch when you copy in dos (/b = binary and /v verify)

set DATE=%date% <- extracts date from system date command
set DAY=%DATE:~7,2% <- extracts two characters starting at 8th (0-7)

COPY /b /v "F:\AccessDatabase\Merchandising.accdb" "C:\Users\Me\Desktop\Merchandising_%DAY%.accdb"

pause
0
etech0Author Commented:
Okay, I fixed that. Now I'm using this, (I changed the destination path) and the copied file is called Merchandising_.accdb.

set DATE=%date% <- extracts date from system date command
set DAY=%DATE:~7,2% <- extracts two characters starting at 8th (0-7)
echo %DAY%

COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB 

Backups\Merchandising_%DAY%.accdb"
pause

Open in new window

0
CSI-WindowsCommented:
Do not use an environment variable with the same name as an internal command.

The following works:

Set VAR=%DATE:~7,2%
Echo %var%
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

wshark83Commented:
try this:

@echo off

set DATE=%date%

set var=%DATE:~7,2%

:loop
if not "%var:~-1%"==" " goto next
set var=%var:~0,-1%
goto loop
:next

COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_%var%.accdb"

pause

the loop remove's any spaces after the number
0
etech0Author Commented:
I changed it to
set WHOLEDATE=%date% <- extracts date from system date command
set DAYOFM=%WHOLEDATE:~7,2% <- extracts two characters starting at 8th (0-7)

COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_%DAYOFM%.accdb"
pause

Open in new window

and I still get a file called Merchandising_.accdb.
0
wshark83Commented:
when you do echo %DAYOFM% and echo %WHOLEDATE% what do you get?

try inserting the loop, my last code works when i test it with a text file....
0
Grasty86Commented:
You shouldnt need to create any variables or anything, just use the variables that windows already has ....

COPY file1.doc file1_%date:~7,2%.doc

This will output the file file1_27.doc
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
etech0Author Commented:
COPY file1.doc file1_%date:~7,2%.doc works on two of my files, but on the third, it says:
ERROR Verify - [filepath of output file]
0
Grasty86Commented:
Maybe the file path is too long? What was the path you were copying to
0
etech0Author Commented:
Hi!
I tried it again, and this time it worked. I don't know why it happened; I guess I'll have to hope it doesn't happen again.
0
etech0Author Commented:
Thanks!

I have another question on this batch file; feel free to see
http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_27655664.html
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.