Disadvantage with conditional nests in Bat

1

I need to create a program that takes the current date and if that date is between the 10th and the 20th of the month, print the date of the last month in YYYYMM format (that is, month -1).

The code I made is the following:

@echo off &setlocal
setlocal EnableDelayedExpansion

SET _test=%date%
SET _month=%_test:~4,2%
SET year=%_test:~10,4%
SET day=%_test:~7,2%

if %day% GTR 10 (

    if %day% LSS 20 (

        IF %_month% EQU 1 (
                set search_month=12
                set /a search_year= %year% -1
        )

        IF %_month% EQU 2 (
                set search_month=01
                set /a search_year= %year%
        )
        IF %_month% EQU 3 (
                set search_month=02
                set /a search_year= %year%
        )

        IF %_month% EQU 4 (
                set search_month=03
                set /a search_year= %year%
        )
        IF %_month% EQU 5 (
            set search_month=04
            set /a search_year= %year%
        )
        IF %_month% EQU 6 (
            set search_month=05
            set /a search_year= %year%
        )

        IF %_month% EQU 7 (
                set search_month=06
                set /a search_year= %year%
        )

        IF %_month% EQU 8 (
                set search_month=07
                set /a search_year= %year%
        )

        IF %_month% EQU 9 (
                set search_month=08
                set /a search_year= %year%
        )

        IF %_month% EQU 10 (
                set search_month=09
                set /a search_year= %year%
        )

        IF %_month% EQU 11 (
                set search_month=10
                set /a search_year= %year%
        )

        IF %_month% EQU 12 (
                set search_month=11
                set /a search_year= %year%
        )

        set /a tosearch = %search_year%%search_month%01
        echo tosearch is %tosearch%

        set /a actualdate = %year%%_month%01
        echo actualdate is %actualdate%


     )
 )

The drawback I have is that, after executing this program, both variables (currentdate and tosearch) are empty. What is the error in the code?

    
asked by thartaras 19.07.2016 в 21:48
source

1 answer

1

I see two errors in your code

the first, that %date% is an untrustworthy variable in BAT code. The reason is that% date% returns the date in a configurable format, each user can configure their system to return the date in a different way, with what type code SET year=%date:~10,4% has many chances to fail miserably.

the second is that with EnabledDelayedExpansion the variables are not expanded, outside the parentheses, with %var% but with !var!

To correct the first problem, you should use another method of obtaining the date. I recommend using WMIC, in a way similar to ...

FOR /F "skip=1 tokens=1-6" %%B IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year ^| findstr /r /v "^$"') DO (     
  echo %%G %%B %%E
)

and to correct the second problem, something like this

  if %%B GTR 10 (
    if %%B LSS 27 (
      IF %%E EQU 7 (
          set search_month=06
          set /a search_year= %%G
      )
    )
  )
  set /a tosearch = !search_year!!search_month!01
)
echo !tosearch!
    
answered by 25.07.2016 в 16:01