Link to home
Start Free TrialLog in
Avatar of ananthu
ananthuFlag for United States of America

asked on

Problem With Date...

All,

    I want the month to come as 01, 02, 03 (i.e., all 1 digit nos. should have a 0 affixed before it...)
   I have written a FOR loop which does not work please help...

   FOR %%mm IN (1 2 3 4 5 6 7 8 9) DO (SET mm=0%mm%)

Ananth.
Avatar of billious
billious

Two leetle problems

1) the control-variable in a FOR loop can only be a single character
2) In a batch, you need to double the "%" before EACH use of the for-loop control-variable.

for %%m in (1 2 3 4 5 6 7 8 9) do (set mm=0%%n)

should do the job.

...but why not use

for %%m in (01 02 03 04 05 06 07 08 09 10 11 12) do (set mm=%%m)

(presuming, of course, you are going to process for all months, and this is but part of your batch (seems pointless otherwise...))

...Bill
Avatar of ananthu

ASKER

Bill Not of Much Help this is my code...

echo %mm%
echo %dd%
set /A m=%mm%
set /A d=%dd%
echo %m%
echo %d%
for %%m in (1 2 3 4 5 6 7 8 9) do (set mm=0%mm%)
for %%d in (1 2 3 4 5 6 7 8 9) do (set mm=0%dd%)
echo %mm%
echo %dd%
ECHO %YYYY%%MM%%DD%

OUTPUT - FOR 18th March 2003
03
17
3
17
017
17
200301717

OUTPUT - FOR 4th March 2003
03
3
3
3
03
3
2003033

Whats Wrong Here?? (dont bother abt the date it is a day behind... i mean the o/p subtracts a day and then displays....)

Ananth
The results that I get are VERY different.

Perhaps it may be of note that your "for %%d..." line sets mm to 0%DD%


Maybe if you revealed what you are trying to do it would be easier than trying to guess what you are expecting the code to do.

For instance, as it stands, your code doesn't actually DO anything with the for-loops - just set a variable.

...Bill


Avatar of ananthu

ASKER

Bill Below is the full code AS IS (Actually more than half of it was taken from one of the questions here...)

-- GETDATE.BAT

@echo off
SET INDATE=%1
IF NOT "%INDATE%"=="" GOTO INPUT
for /f "tokens=2" %%i in ('date /t') do set thedate=%%i

set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%

set /A dd=%dd% - 1

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
set mm=0%mm%
if /I %mm% GTR 0 goto SETDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:SETDAY
if %mm%==01 goto SET31
if %mm%==02 goto LEAP
if %mm%==03 goto SET31
if %mm%==04 goto SET30
if %mm%==05 goto SET31
if %mm%==06 goto SET30
if %mm%==07 goto SET31
if %mm%==08 goto SET31
if %mm%==09 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

goto ERROR

:SET31
set /A dd=31 + %dd%
set dd=0%dd%
goto DONE

:SET30
set /A dd=30 + %dd%
goto DONE

:LEAP
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto DONE

:SET29
set /A dd=29 + %dd%

:DONE
echo %mm%
echo %dd%
set /A m=%mm%
set /A d=%dd%
echo %m%
echo %d%
for %%m in (1 2 3 4 5 6 7 8 9) do (set mm=0%mm%)
for %%d in (1 2 3 4 5 6 7 8 9) do (set mm=0%dd%)
echo %mm%
echo %dd%
ECHO %YYYY%%MM%%DD%
GOTO EXIT

:INPUT
ECHO %1

set temp=
set fyyyy=
set fmm=
set fdd=

:EXIT
What your code does to mm ist in this line:

for %%d in (1 2 3 4 5 6 7 8 9) do (set mm=0%dd%)

it sets mm to the value of dd with an 0 in front. It does this 9 times but only the ninth time is preserved.
it the same as writing

set mm=0%dd%

The line ECHO %YYYY%%MM%%DD% does exactly that:

yyyy=2003
dd = 3
mm = 0%dd% = 03

the whole expression is 2003033

if dd is 17:

mm=0%dd%=017

yymmdd=200301717

However I think you don't understand the IN parameter of the FOR loop: It doesn't match the value of d with whats in parentesis and then executes the SET statment if a match is found. It rather assigns each value of 1 2 ... 9 to d and executes the SET. Of course, every SET overwrites the preceeding result. Furthermore since you don't use %d% in your set statement the for loop is of no use.

Maybe you meant

for %%d in (1 2 3 4 5 6 7 8 9) do set mm=0%%d

But this is of no use either because you get mm=09, the result of the last SET where the value of %%d is 9.

Roger

ASKER CERTIFIED SOLUTION
Avatar of billious
billious

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Should the second last line "goto :eof" read "goto :EXIT" instaed? Or is the "goto :eof" needed to return from the "call :lz %%i"?

Roger
TuliTaivas:

Either will work.

I customarily put a label at the end of a batch, and have done for a number of years. What that label is depends on my mood. Possibly EXIT is not the best name for a label, since it is a batch keyword, and thus can become confused - but it works (and was the label in the original program)

Similarly, many batch-programmers use the same "label on the last line" convention.

In NT+ batch, the label ":eof" is automatically declared to mean 'end of batch program', and it doesn't have to be included in the script.

Note that AFAIAA, you MUST say "goto :eof", and not
"goto eof". If you use the second form, the label eof must be placed explicity in the program.

Noote also that since batch can be recursive, the EXIT command will (I believe) terminate the current batch and any instances caused by recursion (ie. 'STOP - bail out -do no more - enough! whoa!) [I don't use that method, so I'll not swear to it]

The CALL will return when the execution point reaches end-of-file (ie. there are no further commands available to execute)

Note also that call :fred arguments
calls an INTERNAL routing FRED passing arguments as %1, %2...
and call fred arguments
calls an EXTERNAL routing FRED passing arguments as %1, %2...

and that batch-commands are case-insensitive EXCEPT for the control-variable in a FOR loop on NT+ (AFAIAA)

...Bill


Avatar of ananthu

ASKER

Thanks a lot Bill. It worked like a charm. Indeed the one I was working on was pbarrette's code (I did not know where exactly I had lifted it from... and I just checked it :) ). Actually just the IF was what I needed. I am thru with it now. I tried changing that code and even that worked now... Thanks a lot again. The help is appreciated. (and thanks to pbarrette's code too)

Ananth.