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

x
?
Solved

DOS batch to  add prior date to filename on copy

Posted on 2010-11-17
7
Medium Priority
?
907 Views
Last Modified: 2012-06-27
I need to create a dos batch file that will add the PRIOR date  to the filename.   I have automatic file loaded into a network folder.   The file overwrites itself each night.  I need to be able to copy the file to another network folder with the PRIOR date added to the files name.  

I can seem to get it right.  THANKS
0
Comment
Question by:eyes59
7 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 34160139
My article below gives some examples of how to get yesterdays date:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

e.g.
@echo off
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday in YYYYMMDD was %yesterday%
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday%.txt
0
 

Author Comment

by:eyes59
ID: 34160158
To be consistent with the naming convention of the other files that have already been save need the date to be appended as  dd.mm.yyyy    or the file would become    somefile_dd.mm.yyyy.txt
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 664 total points
ID: 34160340
You can either format the string as needed in the VBS part (wscript.echo), or process the result. I'll show the latter:
@echo off
> "%temp%\dateparts.vbs" echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2) 
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
REM below transforms YYYYMMDD into dd.mm.yyyy
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday:~-2%.%yesterday:~-4,2%.%yesterday:~0,4%.txt

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 672 total points
ID: 34160384
If you want to amend the original file you can just re-order the dd mm and yyyy bits and add some dots in the wcript.echo or do as Qlemo has

Steve

@echo off
echo wscript.echo right(100+day(date-1 ),2) ^& "." ^& right(100 + month(date-1),2) ^& "." ^& year(date - 1) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a
echo Yesterday in DD.MM.YYYY was %yesterday%
copy c:\sourcedir\yourfile.txt D:\destdir\somefile-%yesterday%.txt
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 664 total points
ID: 34161219
If you don't mind using a neat little free utility called DOFF, then the code can be pretty simple as chown.

http://www.jfitz.com/dos/index.html#DOFF

~bp

@echo off
for /F "tokens=*" %%A in ('doff dd.mm.yyyy -1') do (
  copy "oldname.ext" "oldname_%%A.ext"
)

Open in new window

0
 
LVL 71

Expert Comment

by:Qlemo
ID: 34375990
This question has been classified as abandoned and is being closed as part of the Cleanup Program.  See my comment at the end of the question for more details.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

885 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