Solved

Need to format the current date to YYYYMMDD

Posted on 2011-09-23
5
780 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 54

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

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.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

734 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