Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

batch file to copy file, appending day of month

Posted on 2012-03-27
11
Medium Priority
?
617 Views
Last Modified: 2012-03-30
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
Comment
Question by:etech0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 6

Expert Comment

by:wshark83
ID: 37771798
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
 
LVL 10

Author Comment

by:etech0
ID: 37771824
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
 
LVL 7

Expert Comment

by:CSI-Windows
ID: 37771880
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 6

Assisted Solution

by:wshark83
wshark83 earned 400 total points
ID: 37771921
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
 
LVL 10

Author Comment

by:etech0
ID: 37772239
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
 
LVL 6

Expert Comment

by:wshark83
ID: 37772279
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
 
LVL 4

Accepted Solution

by:
Grasty86 earned 1600 total points
ID: 37772303
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
 
LVL 10

Author Comment

by:etech0
ID: 37783157
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
 
LVL 4

Expert Comment

by:Grasty86
ID: 37784611
Maybe the file path is too long? What was the path you were copying to
0
 
LVL 10

Author Comment

by:etech0
ID: 37787066
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
 
LVL 10

Author Closing Comment

by:etech0
ID: 37787096
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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick guide on how to use Group Policy to create a custom power plan and set it active on Windows 7.
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

688 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