Solved

DOS batch to  add prior date to filename on copy

Posted on 2010-11-17
7
890 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
Comment Utility
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
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 166 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 43

Accepted Solution

by:
Steve Knight earned 168 total points
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 166 total points
Comment Utility
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 68

Expert Comment

by:Qlemo
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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