Solved

Need to format the current date to YYYYMMDD

Posted on 2011-09-23
5
767 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 51

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

13 Experts available now in Live!

Get 1:1 Help Now