Solved

Need to format the current date to YYYYMMDD

Posted on 2011-09-23
5
776 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 53

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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