Solved

batch file to copy file, appending day of month

Posted on 2012-03-27
11
615 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 6

Assisted Solution

by:wshark83
wshark83 earned 100 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 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

624 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