1.

Solve : Batch loop (Resolved) oops - error fix?

Answer»

Using the Command Prompt in Win XP. Date format is Day mm/dd/yyyy

I want to be able to display the name of the current month using a batch file. The relevant part of the file is:

@echo off
cls

set datemonth=%date:~4,2%
if %datemonth%==11 set datename=Nov

echo %datemonth% %datename%

But do I need 12 'if' statements to handle each of the 12 months or is there a better way with a loop or some kind of array?

Thanks Code: [Select]@echo off
cls

set datemonth=%date:~4,2%

if %datemonth%==1 set datename=January
if %datemonth%==2 set datename=February
if %datemonth%==3 set datename=March
if %datemonth%==4 set datename=April
if %datemonth%==5 set datename=May
if %datemonth%==6 set datename=June
if %datemonth%==7 set datename=July
if %datemonth%==8 set datename=August
if %datemonth%==9 set datename=September
if %datemonth%==10 set datename=October
if %datemonth%==11 set datename=November
if %datemonth%==12 set datename=December

echo %datemonth% %datename%
echo.
pause
I just thought I'd write the code for 12 'if' statements. I don't think there is a better way but I'm only guessing. Also just out of curiosity, how did you work out the '%date:~4,2%. I mean, I know %date% shows the full date so how did you get the ':~4,2' in the LINE below?

set datemonth=%date:~4,2%

Quote from: jjbtcp on November 10, 2007, 04:00:17 PM

just out of curiosity, how did you work out the '%date:~4,2%. I mean, I know %date% shows the full date so how did you get the ':~4,2' in the line below?

set datemonth=%date:~4,2%

I'm definitely not the best one to explain that but in my understanding Date environment variable is stored as an 12-byte FIELD in the format such as Sun 11112007 and is obviously updated automatically when the clock goes past midnight.

So %date=:~4,2% skips the first 4 bytes (Sun + space) and returns the next two bytes. i.e. 11

To select the year one would use %date=:~8,4% i.e. skip 8 bytes and return the next 4 making it 2007. (Or %date:~-4% would give the same result).

Some instructions are here.

I hope I've not made a fool of myself with the above explanation, my technical knowledge is not all that good.

Thanks for the 12 IF lines, I already use that solution but looking for something a bit more sophisticated!!



I don't understand your explanation that well. But it's not you, it's me, My technical knowledge is very advanced but I haven't got around to learning the 'Bits and Bytes' of Computers.

Sorry I couldn't help much. Thanks for explaining.
The completed file to display day, date, month and year using a For loop to give the month a name is shown below for anyone interested. The For loop was described on another forum..

Code: [Select]@echo off
cls

set dateday=%date:~0,3%
set datemonth=%date:~4,2%
set datenum=%date:~7,2%
set dateyear=%date:~10,4%

FOR /F "TOKENS=%datemonth%" %%a IN (
"JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") DO (
set month=%%a)
echo.
echo.
echo Day = %dateday% Date = %datenum% Month = %month% Year = %dateyear%
Quote
The For loop was described on another forum..

Can you give me the link to the post it was discussed in, please?

Oh and I understand your explanation now... Never say sleeping over something won't work.Further testing has shown that an error occurs when datemonth is greater than 07 and the leading digit is 0 (AUG & SEP) so the set datemonth line should read:

set/a datemonth=1%date:~4,2%-100

Apologies all round - testing continues.

What does the '-100' do?Quote
set/a datemonth=1%date:~4,2%-100
Quote
What does the '-100' do?

Oh dear - bear with me on this one.

It's not just the -100 which must be considered. You'll see that the original set is now set/a and there is a 1 prior to %date:~.....

When Set/a detects a number beginning with 0 it uses the Octal (base eight) system. When considering August and September the month numbers 08 and 09 are invalid so Set fails.

Set/a allows arithmetic calculations to be done.

The 1 prior to %date:~... means that 1 is to be prefixed to the environment variable %datemonth%. When the month is August (08) %datemonth% would be set to 108. Then the arithmetic CALCULATION -100 is carried out and %datemonth% now becomes 8.

I understand that without the leading 0 Set now defaults to the Decimal system (base ten).





Cool, This is quite a good script, I would have never thought of it myself.


Discussion

No Comment Found