Solved

DOS batch to  add prior date to filename on copy

Posted on 2010-11-17
7
897 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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