Solved

Need to format the current date to YYYYMMDD

Posted on 2011-09-23
5
777 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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