Solved

Need to format the current date to YYYYMMDD

Posted on 2011-09-23
5
772 Views
Last Modified: 2012-05-12
I have a batch file that is going to be called automatically.  It looks for a file that will have the current date as part of the file name.  I need a DOS script that can take the current date, transform it to the format YYYYMMDD and then append that to the base of the file name in order to process the file.  As an example, the base of the file name is ABC- so the script needs to get the current date and append it as follows:

ABC-20110923.csv

I have been trouble finding information on how to do that.

Thanks for your help.
0
Comment
Question by:techhound
  • 3
5 Comments
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 36587220
From a question I am answering at the moment.  have a look at my article on this subject below too:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html


http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27323611.html?cid=1573#a36586727

@echo off  
REM See http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html for dates info.  
REM Steve Knight (http://scripts.dragon-it.co.uk)  
REM Gets two bits from vbscript -- day and yyyy-mm-dd  
 
set log=c:\logfile.txt  
set script="%temp%\dateparts.vbs"  
  echo wscript.echo year(date) ^& right(100 + month(date),2) ^& right(100+day(date),2) > %script%  
  for /f "tokens=1 delims=," %%a in ('cscript //nologo %script%') do (set yyyymmdd=%%a)
del %script%  

echo Variable %yyyymmdd% is the date now.

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36587231
The "easy" way as shown involving %date% is difficult when using different machines, OS's or logins as the format changes.  VBScript will work the same every time.
0
 
LVL 5

Accepted Solution

by:
DerZauberer earned 250 total points
ID: 36587307
I have a german windows, so the date format is 23.09.2011. Maybe you have to change the start indices inside the string.

echo %date%
set name=ABC-%date:~6,4%%date:~3,2%%date:~0,2%.csv
echo %name%
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36587410
Which proves my point :-)
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 36587749
Steve's article has some great approaches to this.  

I'll just add that I often use a tiny free utility called DOFF for this which can be found at:

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

If that's any interest to you and you have questions let me know.

~bp

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find replace in windows 10 9 84
Rename files on Windows 10 44
old Toshiba laptops supporting both MS Dos and Win 95 22 168
how to run a batch process 16 43
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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

12 Experts available now in Live!

Get 1:1 Help Now