There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
DOS/PDA/Other
Tag Cloud
adware audio bios blue screen boot bsod card computer crash dell driver drivers error excel firefox freeze google hard drive hardware hijackthis install internet laptop linux malware network no sound outlook problem reboot recovery redirect router screen server slow sound speakers spyware startup trojan usb video virus vista windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/PDA/Other >
Solved: Date format in bat file

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
bikey33's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Jun 2009
Experience: Intermediate
21-Jun-2009, 12:07 AM #1
Solved: Date format in bat file
Hi,
I need to have a date reformatted so that I can see if a file is in a directory for the current date.

When I run the following:
@echo off > %temp%\# & setLocal EnableDelayedExpansion
echo %date%

I get this:
2009-06-20

I need it to be 2009/06/20

Once i have that I know how to do the rest. I have searched and tried many examples that parse the date out to reformat and nothing seems to work, so I am back to square one.

Your help would be tremendously appreciated!

Thanks
devil_himself's Avatar
Distinguished Member with 4,921 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
21-Jun-2009, 04:01 PM #2
Code:
@echo off
for /f "tokens=1-3 delims=./-" %%f in ("%date%") do (
    set _today=%%f/%%g/%%h
    echo %_today%
 )
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
21-Jun-2009, 06:44 PM #3
If the delimiter will always be a -:
Code:
Set _Today=%date:-=/%
Echo %_Today%
If you want it to be 20090620, use this:
Code:
Set _Today=%date:-=%
Echo %_Today%
Jerry
bikey33's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Jun 2009
Experience: Intermediate
21-Jun-2009, 10:12 PM #4
Hi there,
That worked great! I just needed to put echo %_today%
outsided of the parenthesis instead of in. Thank you so much. Now I have another problem, as when I tried to implement it on the server (I was using a test one), the current date comes up as:
The current date is: Sun 06/21/2009.

Is there a way to get rid of the SUN? I can change other variables around (set _today=%%h-%%f-%%g) easily thanks to you :-), but don't know what to do about that Day variable.

Thanks,
Bikey33
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
21-Jun-2009, 10:32 PM #5
Code:
@Echo Off
If "%Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
For /F "Tokens=%_NumTok% Delims=./- " %%f In ("%date%") Do Set _Today=%%f/%%g/%%h
)
Echo %_Today%
Added the line in Red and also added a space after Delims.

This will get the date regarless of your Regional settings, and sets the _fdate variable to YYYYMMDD. You can then extract the year, month, and day from _fdate and put it together anyway you want:
Code:
@Echo Off
Set _Date=%date%
If "%_Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%_Date%") Do Set _fdate=%%B%%C%%D
Set _Today=%_fdate:~4,2%-%_fdate:~6,2%-%_fdate:~0,4%
Echo %_Today%
HTH

Jerry
ghostdog74's Avatar
Senior Member with 126 posts.
 
Join Date: Dec 2005
21-Jun-2009, 10:35 PM #6
to use date independent of regional settings,you can use vbscript
Code:
strtoday = Now
yr = Year(strtoday)
mth = Month(strtoday)
dy = Day(strtoday)
If Len(mth) <2 Then 
	mth="0"&mth
End If
If Len(dy) <2 Then 
	dy="0"&dy
End If
thedate = yr &"-"&mth&"-"&dy
WScript.Echo thedate
save the above as myscript.vbs and in your batch
Code:
for /f ..... ( 'cscript /nologo myscript.vbs' ) do (
  .....
)
(Or you can do your task entirely in vbscript)
bikey33's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Jun 2009
Experience: Intermediate
30-Jun-2009, 11:09 PM #7
Hi, Sorry it took so long to reply. Had to focus on other issues and could not test this right away. I used the resolution from The Outcast and it worked great. Thank you so much!
TheOutcaste's Avatar
Computer Specs
Distinguished Member with 5,315 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
30-Jun-2009, 11:48 PM #8
You're Welcome!

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post)



Jerry
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 02:36 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.