0

I want to have some code like this:

if %date% equ "Mon" echo do this do that

but the cmd window closes after encountering this code, even if I put

pause

after it. How do I fix this?

Zenedus
  • 96
  • 9
  • Just echo the `date` variable at the command prompt. Do you think that comparison will ever be equal regardless of you forgetting to put quotes around the variable. The proper way to debug a batch file is to run the batch file from the cmd prompt. Do not run it with your mouse. – Squashman Jun 04 '18 at 21:48
  • But what if I want to say: if %date% equ "Mon" echo goto school – Zenedus Jun 04 '18 at 21:54
  • 1
    You are doing a string comparison. If you have quotes on one side of the comparison you must have quotes on the other side of the comparison. The date variable is region dependent. Your date variable will not display the same as mine depending on which countries we each live in. – Squashman Jun 04 '18 at 21:56
  • 1
    You probably want to use `IF /I "%DATE:~,3%"=="Mon"` or possibly `IF NOT "%DATE:Mon=%"=="%DATE%"`, however neither are safe or robust methods in anything other than your specific current user environment. – Compo Jun 04 '18 at 22:21
  • My %date% doesn't even have any day of week – phuclv Jun 05 '18 at 01:47
  • Possible duplicate of [Setting a windows batch file variable to the day of the week](https://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week) – phuclv Jun 05 '18 at 01:47

2 Answers2

0

You probably want to use:

IF /I "%DATE:~,3%"=="Mon" (Echo Do this
    Echo Do that)

Or possibly:

IF NOT "%DATE:Mon=%"=="%DATE%" (Echo Do this
    Echo Do that)

However neither of those are safe or robust methods in anything other than your specific current user environment.

This is how I'd get the day of the week into a variable using a batch file with WMIC:

For /F %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek') Do For %%B In (
    Monday.1 Tuesday.2 Wednesday.3 Thursday.4 Friday.5 Saturday.6 Sunday.0
) Do If "%%~xB"==".%%A" Set "WDName=%%~nB"

Line 2 can be optionally adjusted to start with Sunday.0 Monday.1 etc. if necessary or Lunes.1 Martes.2 etc. depending upon your language.

You could then use:

If "%WDName%"=="Monday" (Echo Do this
    Echo Do that)

Although (Get-Date).DayOfWeek in PowerShell seems so much simpler.

Compo
  • 36,585
  • 5
  • 27
  • 39
0

Here's a complete cmd file that will give you what you need. The important bit is all in the getDow function and, hopefully, it's commented well enough to understand. First, the test harness:

@echo off
rem Test harness bit - just get current date and compare with getDow.

date /t
call :getDow num long short
echo Day of week is %num%, %long%, %short%
goto :eof

The function itself is:

rem Usage: call :getDow <num> <long> <short>
rem        <num> will receive the numeric form (0-6).
rem        <long> will receive the long form (e.g., Monday).
rem        <short> will receive the short form (e.g., Mon).

:getDow
    rem Create local scope to prevent information leakage.

    setlocal enableextensions enabledelayedexpansion

    rem Create array for translation.

    set idx=0
    for %%a in (Sunday Monday Tuesday Wednesday Thursday Friday Saturday) do (
        set dow[!idx!]=%%a
        set /a "idx += 1"
    )

    rem Get the numeric day of week, mmi command will
    rem output 'DayOfWeek=N' and we just extract N.

    for /f "tokens=2 delims==" %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /value ^| findstr "DayOfWeek="') do (
        set localNum=%%a
    )
    set localStr=!dow[%localNum%]!

    rem Properly end scope but let selected information leak.

    endlocal&&set %1=%localNum%&&set %2=%localStr%&&set %3=%localStr:~0,3%
    goto :eof

A sample run of that script gives:

Tue Jun 05
Day of week is 2, Tuesday, Tue
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • That uses the same method as my already posted answer, only it requires delayed expansion, and uses a whole lot more lines. _(mine's a single line split into 3 for readability and easier modification by the end user!)_ – Compo Jun 05 '18 at 08:51
  • @Compo, I'm sure if yours is better, it will garner more votes, I'm equally sure I could point out faults in *your* answer were I so inclined :-) Myself, I prefer to have general purpose functions for all this sort of stuff so it's easy to code up. – paxdiablo Jun 05 '18 at 09:37
  • I have not picked out any faults in your answer, have I? _(mentioning that it uses a whole lot more lines does not constitute a fault nor was it implied)_. But, I'd be happy were you to explain to me the faults in my code, in order that I learn from them moving forward, thank you. – Compo Jun 05 '18 at 09:51
  • @Compo, apologies if I misread, "too long" certainly seems like a criticism :-) On your own answer, I think you may need to change the values checked, I'm pretty certain the DOW values from WMI are 0 thru 6 rather than 1 thru 7. Other than that, it's fine. – paxdiablo Jun 05 '18 at 10:09
  • 2
    Thank you for that, even though I've used the code over a good few years, I've obviously never had a need to use it on my weekend and was unaware of the error in it. I have therefore adjusted my answer accordingly. – Compo Jun 05 '18 at 10:33