1

I am attempting to write a batch file that will, eventually, create a file with a name based upon the current month. However, I have already encountered some problems. I am attempting to use a if/elseif statements to set a variable that contains the month name as a string with no success. It merely echos "" to the screen instead of the month name.

    @echo OFF
set month-num=%date:~4,2%
if "%month-num%" == "01" then set month_txt="January" else if "%month-num%" == "02" then set month_txt="February" else if "%month-num%" == "03" then set month_txt="March" else if "%month-num%" == "04" then set month_txt="April" else if "%month-num%" == "05" then set month_txt="May" else if "%month-num%" == "06" then set month_txt="June" else if "%month-num%" == "07" then set month_txt="July" else if "%month-num%" == "08" then set month_txt="August" else if "%month-num%" == "09" then set month_txt="September" else if "%month-num%" == "10" then set month_txt="October" else if "%month-num%" == "11" then set month_txt="November" else if "%month-num%" == "12" then set month_txt="December"
@echo "%month_txt%"
timeout /t -1

I would really appreciate any guidance; I am not too familiar with this form of programming.

KellyM
  • 2,472
  • 6
  • 46
  • 90
  • Try to change the name of the variable, so it uses _ instead of - – Ibrahim Aug 26 '16 at 21:12
  • Unfortunately, that did not work. Thanks though. – KellyM Aug 26 '16 at 21:14
  • There is not such `elseif` command in Batch. Use:`else if` – Aacini Aug 26 '16 at 21:15
  • Unfortunately, that still did not work either. Thanks though. I will updated the opening post with the modifed code. – KellyM Aug 26 '16 at 21:18
  • There is not `then` word in Batch. You must also need to enclose in parentheses _each part_ that comprises the "then" part; for example: `if "%month-num%" == "01" (set month_txt="January") else if "%month-num%" == "02" (set month-txt="February") else if ...` – Aacini Aug 26 '16 at 21:26
  • Finally, you have an error in the position of the month. If the date format is `DD/MM/YYYY` then the month is at substring `%date:~3,2%`, _not_ 4... – Aacini Aug 26 '16 at 21:34

2 Answers2

2

I suggest you to use another method to get the month name; for example, via an array:

@echo OFF
setlocal EnableDelayedExpansion

rem Initialize month names based on two-digits numbers
set i=100
for %%a in (January February March April May June July August September October November December) do (
   set /A i+=1
   set month[!i:~1!]=%%a
)

set month-num=%date:~3,2%

set month-txt=!month[%month-num%]!

echo "%month-txt%"
timeout /t -1
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

If you are able to add a script to your machine from the Internet, I have been successful using getTimestamp.bat from http://www.dostips.com/forum/viewtopic.php?t=4847.

C:>type t.bat
CALL getTimestamp.bat -F "{MONTH}"

FOR /F "usebackq tokens=*" %%m IN (`getTimestamp.bat -F "{MONTH}"`) DO (SET "MONTH_NAME=%%m")
ECHO MONTH_NAME is set to %MONTH_NAME%

The result of running it is:

 9:34:08.95  C:\src\bat
C:>call t.bat

 9:34:15.87  C:\src\bat
C:>CALL getTimestamp.bat -F "{MONTH}"
August
MONTH_NAME is set to August
lit
  • 14,456
  • 10
  • 65
  • 119