0

I don't understand why this script doesn't seem to work and doesn't even pause to see where the error is.. Here is the code for the batch file:

I want this batch to output a series of birthdays starting from Ihf0101 till Ihf3112

set /a month=1
:m
if %month% leq 12 
(
    set /a day=1
    :d
    if %day% leq 31 
    (
      if %day% leq 9   set birthday=Ihf0%day%
      if %day% gtr 9   set birthday=Ihf%day%
      if %month% leq 9 
    (
      set birthday=%birthday%0%month%
      echo %birthday%
      )
     if %month% gtr 9 
    (
      set birthday=%birthday%%month%
      echo %birthday%
     ) 
      set /a day+=1
      goto :d
     )

      set /a month+=1
      goto :m
)
pause
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Please use [proper formatting](https://stackoverflow.com/help/formatting)! Anyway, labels and `goto` cannot be used in parenthesised blocks, because they break the block context... – aschipfl Nov 20 '17 at 09:51
  • it was written in a proper way, the editor in this site manipulated them.. the format is correct, but there must be a syntax or logical error somewhere! – Abdulrahman Mahdy Nov 20 '17 at 09:56
  • ohhhhhh you mean I can't basically use labels such as (:m or :d) within an If statement because it is considered as a block context ??? – Abdulrahman Mahdy Nov 20 '17 at 10:06
  • 1
    You should at least read the help of `IF /?`. The opening parenthesis has to be on the same line. The goto breaks the blocks, but that doesn't matter here. But you need to use delayed expansion to avoid expansion problems – jeb Nov 20 '17 at 10:22

1 Answers1

0

Use for /L loops instead:

@echo off
setlocal enabledelayedexpansion
set "y=2017"
for /l %%m in (101,1,112) do (
  set "m=%%m"
  for /l %%d in (101,1,131) do (
    set "d=%%d"
    xcopy /d:!m:~1!-!d:~1!-%y% /l . .. >nul 2>&1 && echo lhf!d:~1!!m:~1!
  )
)

two little tricks:
- counting from 101 to 131 and taking the last two digits only gives you a two-digit format always (with leading zero for numbers below 10)
- using xcopy to check if the date is really existent (I learned that here) (necessary to know the year for proper calculation of leap-years)

Stephan
  • 53,940
  • 10
  • 58
  • 91