| 1. |
Solve : VBS Date & Time Accessible from a batch? |
|
Answer» Some weeks ago I hacked together a crude batch file which created a VB script & then read back the results to make various VBS date stuff accessible in a batch. I was gratified to see people make use of it and since I am on Xmas break from work I decided to tidy it up a bit and add some features as you can see below Happy holidays to everyone. Except for this guy, who is "Off to Guantanamo!" Do you supply your own orange overall? Have a great time. Thank you for this. results can be open automaticaly when you run the bat file?Quote from: DAan on January 04, 2009, 12:18:15 AM Thank you for this. What do you mean? DAan probably is trying to ask if the BAT file displays the date/time when it is finished running.Quote from: Helpmeh on January 05, 2009, 03:18:15 PM DAan probably is trying to ask if the BAT file displays the date/time when it is finished running. Well, if so, then all I can say is that it should be obvious to the intended users (either from looking at the "output" box above, or by actually saving and running the batch file) what it does. Also people who have some experience of writing & using batch files will be able to see how to modify it for their own purposes. Quote from: Dias de verano on January 05, 2009, 03:35:21 PM Quote from: Helpmeh on January 05, 2009, 03:18:15 PMDAan probably is trying to ask if the BAT file displays the date/time when it is finished running. True....Dias. Is it possible to enter a date and have your script produce the results from that date. If 23-11-2008 was entered can your code be modified to display month, day, year, week#, day# etc using that date as input? I have searched T.Salmi, alt.msdos and others without success. Cheers Yes. It is 8:08 AM here and I am off to work, I will answer after work this afternoon. You should be looking at VBS date functions really. Quote from: Hedonist on January 27, 2009, 01:01:53 AM Dias. Is it possible to enter a date and have your script produce the results from that date. If 23-11-2008 was entered can your code be modified to display month, day, year, week#, day# etc using that date as input? Qdate.bat Code: [Select] @echo off REM Create VBS script set vbsfile=DateInfo.vbs echo Newdate = WScript.Arguments(0)>%vbsfile% echo if IsDate(Newdate) then>>%vbsfile% echo Newdate = CDate(Newdate)>>%vbsfile% echo DateYear = DatePart("YYYY", Newdate)>>%vbsfile% echo DateMonth = DatePart("M" , Newdate)>>%vbsfile% echo DateDay = DatePart("D" , Newdate)>>%vbsfile% echo WeekOfYear = DatePart("WW" , Newdate)>>%vbsfile% echo DayOfYear = DatePart("Y" , Newdate)>>%vbsfile% echo WeekDayNumber = DatePart("W" , Newdate)>>%vbsfile% echo TodayNameShort = WeekdayName(WeekDayNumber,True)>>%vbsfile% echo TodayNameFull = WeekdayName(WeekDayNumber,False)>>%vbsfile% echo MonthNameShort = MonthName(DateMonth,True)>>%vbsfile% echo MonthNameLong = MonthName(DateMonth,False)>>%vbsfile% echo Wscript.Echo DateYear^&" "^&DateMonth^&" "^&DateDay^&" "^&Week^ OfYear^&" "^&DayOfYear^&" "^&WeekDayNumber^&" "^&Today^ NameShort^&" "^&TodayNameFull^&" "^&MonthNameShort^&" "^&MonthNameLong>>%vbsfile% echo else>>%vbsfile% echo Wscript.Echo "ERROR">>%vbsfile% echo end if >>%vbsfile% REM below is example batch code to call the vbs REM Example of use REM 1) get a date string into a variable set /p Querydate=Enter Date: REM 2) pass the string to the VBS script and REM parse the output, which will either be 10 REM items of data, or else the string "ERROR" REM if the string is not a valid date. REM REM Store output of vbs script in variables for /f "tokens=1-10 delims= " %%A in ('cscript //nologo %vbsfile% %querydate%') do ( set Year=%%A set Month=%%B set Day=%%C set WeekNumber=%%D set DAYNUMBER=%%E set DayOfWeekNum=%%F set DayNameShort=%%G set DayNameLong=%%H set MonthNameShort=%%I set MonthNameLong=%%J ) REM 3) Do something with the data if "%Year%"=="ERROR" ( echo Error: invalid date ) else ( Echo Data for Date: %Querydate% echo. echo Date Year : %Year% echo Date Month : %Month% echo Date Day : %Day% echo Week Of Year : %WeekNumber% echo Day Of Year : %DayNumber% echo Day Of Week [Number] : %DayOfWeekNum% echo Day Of Week [Short Name] : %DayNameShort% echo Day Of Week [Full Name] : %DayNameLong% echo Month [Short Name] : %MonthNameShort% echo Month [LONG Name] : %MonthNameLong% echo. Echo %DayNameLong%, %MonthNameLong% %Day%, %Year% is/was/will be in week %WeekNumber% echo. ) Alternatively you could just save the vbs script somewhere on your PATH and call it from batches Code: [Select] Newdate = WScript.Arguments(0) if IsDate(Newdate) then Newdate = CDate(Newdate) DateYear = DatePart("YYYY", Newdate) DateMonth = DatePart("M" , Newdate) DateDay = DatePart("D" , Newdate) WeekOfYear = DatePart("WW" , Newdate) DayOfYear = DatePart("Y" , Newdate) WeekDayNumber = DatePart("W" , Newdate) TodayNameShort = WeekdayName(WeekDayNumber,True) TodayNameFull = WeekdayName(WeekDayNumber,False) MonthNameShort = MonthName(DateMonth,True) MonthNameLong = MonthName(DateMonth,False) Wscript.Echo DateYear&" "&DateMonth&" "&DateDay&" "&WeekOfYear&" "&DayOfYear&" "&WeekDayNumber&" "&TodayNameShort&" "&TodayNameFull&" "&MonthNameShort&" "&MonthNameLong else Wscript.Echo "ERROR" end if Code: [Select] S:\Test\Batch\querydate>qdate.bat Enter Date:23-11-2008 Data for Date: 23-11-2008 Date Year : 2008 Date Month : 11 Date Day : 23 Week Of Year : 48 Day Of Year : 328 Day Of Week [Number] : 1 Day Of Week [Short Name] : Sun Day Of Week [Full Name] : Sunday Month [Short Name] : Nov Month [Long Name] : November Sunday, November 23, 2008 is/was/will be in week 48 S:\Test\Batch\querydate>qdate.bat Enter Date:34-9-1788 Data for Date: 34-9-1788 Error: invalid date |
|