Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 619
  • Last Modified:

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

0
etech0
Asked:
etech0
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now