0

I am trying to do a FOR /L loop for a class of mine i have gotten it to loop the 12 times but its not changing the MonthName variable to display each month. what i want it ot so is take the current month number and execute the loop that many times with the months of the year like for the first time through the loop i want it to display 1. January then 2. Febuary and so on till it reaches the current month here is the code i have so far and a sample execution i can only get it to display the current month's name

rem @echo off 
setlocal enabledelayedexpansion
set CurrentMonth=%date:~4,2%
IF %currentMonth% EQU 08 (
         set /a currentMonth=8
         )
IF %currentMonth% EQU 09 (
         set /a currentMonth=9
       ) 
 set Mmap=1-Januray;2-Febuary;3-March;4-April;5-May;6-June;7-July;8-August;9-September;10-October;11-November;12-December;
FOR /L %%x in (1,1,!CurrentMonth!) DO  (
           call set MonthName=%%Mmap:*%%x-=%%
           set MonthName=%MonthName:;=&rem.%
           echo %%x !MonthName! )

C:\Users\cis106stu\BATCHFILES5>FORLOOP
1 December
2 December
3 December
4 December
5 December
6 December
7 December
8 December
9 December
10 December
11 December
12 December

C:\Users\cis106stu\BATCHFILES5>
Stephan
  • 53,940
  • 10
  • 58
  • 91
Marky2019
  • 47
  • 6
  • 2
    Please review your question, the code posted does not produce your output – PA. Dec 08 '19 at 18:37
  • FYI, the method you are using for the retrieval of the date, is dependent upon the User's and/or PC's configuration. If you're happy to use it still, I'd suggest that `%DATE:~-10,2%` would be successful on more PC's than `%DATE:~4,2%`, as many locales do not prepend with the short day name. – Compo Dec 08 '19 at 23:49

3 Answers3

1

As you insist on using a for /l loop:

@echo off 
setlocal enabledelayedexpansion
set /a CurrentMonth=1%date:~4,2%-100

set "Mmap=;1-Januray;2-Febuary;3-March;4-April;5-May;6-June;7-July;8-August;9-September;10-October;11-November;12-December;"

FOR /L %%x in (1,1,!CurrentMonth!) DO  (
  set MonthName=!Mmap:*;%%x-=!
  for /f "delims=;" %%y in ("!MonthName!") do echo %%x %%y 
)

I added a leading ; to Mmap, used another method to split the string and took advantage of delayed expansion (why not use it when you already enabled it)

(Note: the format of %date% is highly user configurable, so %date:~4,2% may extract the wrong string. It works for the format DDD MM/dd/yyyy and has to be adapted for other formats (better use a settings-independent solution))

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you for the updated answer that works great i apperciate what you did thank yous again – Marky2019 Dec 09 '19 at 20:55
  • 1
    When your issue is solved, please [pick the answer that helped you most](https://stackoverflow.com/help/someone-answers) to remove the question from the "open" queue. – Stephan Dec 09 '19 at 21:36
0

Let me suggest an alternative approach (both with correcting the date format and with the extraction from the string Mmap:

@echo off 
setlocal 
set /a CurrentMonth=1%date:~4,2%-100

set "Mmap=1-Januray;2-Febuary;3-March;4-April;5-May;6-June;7-July;8-August;9-September;10-October;11-November;12-December"

for %%a in (%Mmap%) do (
  for /f "tokens=1,2 delims=-" %%b in ("%%a") do (
    if %%b leq %CurrentMonth% echo %%b %%c
  )
)

(Note: the format of %date% is highly user configurable, so %date:~4,2% may extract the wrong string. It works for the format DDD MM/dd/yyyy and has to be adapted for other formats (better use a settings-independent solution))

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thank you for you answer it makes sense but i need to use a for /L loop to do it and also thank you for editing my question in the first place i tried to get to look like that but i am a novice when it comes to computers now i just started this past summer – Marky2019 Dec 09 '19 at 01:48
  • Just an assumption I made @Stephan, but I thought `%date:~4,2` was correctly choosing the month based upon this format `ddd mm/dd/yyy`. – Compo Dec 12 '19 at 00:21
  • @Compo: you are probably right. Changed to OP's numbers and adapted the Note. – Stephan Dec 12 '19 at 08:35
0

Just use a for /l loop and loop 12 times to get the variables we've set and wmic to determine the month you're in.

@echo off
set "m1=January" & set "m2=February" & set "m3=March" & set "m4=April" & set "m5=May" & set "m6=June" & set "m7=July" & set "m8=August" & set "m9=September" & set "m10=October" & set "m11=November" & set "m12=December"
for /f %%i in ('wmic path win32_localtime get month ^| findstr /r /c:[1-12]') do for /l %%a in (1,1,12) do if %%a leq %%i call echo %%a %%m%%a%%%

The above does not require the CurrentMonth variable to be set as the metavariable %%i has that value. If you however still want to set the variable, you can:

@echo off
set "m1=January" & set "m2=February" & set "m3=March" & set "m4=April" & set "m5=May" & set "m6=June" & set "m7=July" & set "m8=August" & set "m9=September" & set "m10=October" & set "m11=November" & set "m12=December"
for /f %%i in ('wmic path win32_localtime get month ^| findstr /r /c:[1-12]') do set /a CurrentMonth=%%i
for /l %%a in (1,1,12) do if %%a leq %CurrentMonth% call echo %%a %%m%%a%%%
Gerhard
  • 22,678
  • 7
  • 27
  • 43