Solved

batch file to copy file, appending day of month

Posted on 2012-03-27
11
600 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
  • 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
 
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
Backup Your Microsoft Windows Server®

Backup 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

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now