Solved

DOS batch to  add prior date to filename on copy

Posted on 2010-11-17
7
901 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 69

Assisted Solution

by:Qlemo
Qlemo earned 166 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 43

Accepted Solution

by:
Steve Knight earned 168 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 53

Assisted Solution

by:Bill Prew
Bill Prew earned 166 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 69

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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 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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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