There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
DOS/Other
Tag Cloud
access acer asus bios bsod computer crash driver drivers error ethernet excel freeze gaming google gpu hard drive hardware hdmi internet laptop malware memory missing monitor motherboard network operating system printer problem ram registry router slow software sound trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > DOS/Other >
Solved: Date format in bat file

Reply  
Thread Tools
bikey33's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Jun 2009
Experience: Intermediate
21-Jun-2009, 01: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,922 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
21-Jun-2009, 05: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 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
21-Jun-2009, 07: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, 11: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 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
21-Jun-2009, 11: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
Member with 146 posts.
 
Join Date: Dec 2005
21-Jun-2009, 11: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
01-Jul-2009, 12:09 AM #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 9,048 posts.
 
Join Date: Aug 2007
Location: Oregon, USA
Experience: Intermediate
01-Jul-2009, 12:48 AM #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
Reply

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.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:43 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.