Check day and if day = .. start file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
malortje
Posts: 2
Joined: 14 Jul 2017 04:15

Check day and if day = .. start file

#1 Post by malortje » 14 Jul 2017 04:26

Hi Guys,

I've found a nice peace of code for checking the day time here.

Code: Select all

@ECHO off
    set v_day_of_week=
    set v_day=
    set v_month=
    set v_year=

    SETLOCAL ENABLEEXTENSIONS
      for /f "tokens=1" %%t in ('date/t') do (
        set v_day_of_week=%%t
        if "%%ta" LSS "a" (set toks=1-3) else (set toks=2-4)
      )
::DEBUG echo toks=%toks%
      for /f "tokens=2-4 delims=(-)" %%a in ('echo:^|date') do (
::DEBUG echo first token=%%a
        if "%%a" GEQ "A" (
          for /f "tokens=%toks% delims=.-/ " %%i in ('date/t') do (
            set '%%a'=%%i
            set '%%b'=%%j
            set 'yy'=%%k
          )
        )
      )
      if %'yy'% LSS 100 set 'yy'=20%'yy'%
      set Today=%'yy'%-%'mm'%-%'dd'%

    ENDLOCAL & SET day_of_week=%v_day_of_week% & SET v_year=%'yy'%& SET v_month=%'mm'%& SET v_day=%'dd'%

    ECHO Today is Year: [%V_Year%] Month: [%V_Month%] Day: [%V_Day%]
    set datestring=%V_Year%%V_Month%%V_Day%
    echo %datestring%
    echo day of week=%day_of_week%

    :EOF


I've a list of a few programs that I want to start everyday. But just 1 program in that list must only start up on Monday.

the echo day of week=%day_of_week% will return the first 2 digits of the day.
Is there anything possible to do this with a IF statement ?

Thanks in advance!

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Check day and if day = .. start file

#2 Post by pieh-ejdsch » 15 Jul 2017 00:15

Hello malortje,

Robocopy shows a summary of the start time in the header.
This line should also contain the day of the week (depending on the language).

A good usable time stamp can be found in every error line, which looks the same on every system.

Code: Select all

@echo off
setlocal
:: rem Starttime
@robocopy /L |find ". "

:Timestamp
 rem robocopy /L "\.. Timestamp ..\\" .
for /f "eol=D tokens=1-6 delims=/: " %%T in (
 'robocopy /L  /njh "\|" .^|find "123" '
 ) do (
  set "TS=%%T%%U%%V-%%W%%X%%Y"
  set "TSDATE=%%T%%U%%V"
  set /a YY=%%T , MM=100%%U %%100 , TT=100%%V %%100
)
:: end Timestamp

:: Weekday is
for /f "tokens=2 delims=, " %%D in ('robocopy /l ^|find ". "') do echo %%D

 rem run only Monday
@robocopy /L |find ". " |find /i "monday"
if NOT errorlevel 1 (
  rem do somthing
)


Of course you can also use getTimestamp.bat or jTimestamp.bat.

Phil

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Check day and if day = .. start file

#3 Post by Compo » 15 Jul 2017 04:39

You could simplify the entire process by just using the built-in WMIC.exe.

Code: Select all

For /F EOL^=D %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek'
) Do If %%A Equ 1 Start "" "Your Program.exe"
You just need to verify that Monday is allocated 1 in your local output, it is here despite it being the second day of the week.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Check day and if day = .. start file

#4 Post by aGerman » 15 Jul 2017 07:37

Compo wrote:You just need to verify that Monday is allocated 1 in your local output, it is here despite it being the second day of the week.

No need for doing that :wink:
https://msdn.microsoft.com/en-us/library/aa394171(v=vs.85).aspx
By convention, the value 0 is always Sunday, regardless of the culture or the locale set on the machine.

Steffen

malortje
Posts: 2
Joined: 14 Jul 2017 04:15

Re: Check day and if day = .. start file

#5 Post by malortje » 17 Jul 2017 05:31

Compo wrote:You could simplify the entire process by just using the built-in WMIC.exe.

Code: Select all

For /F EOL^=D %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek'
) Do If %%A Equ 1 Start "" "Your Program.exe"
You just need to verify that Monday is allocated 1 in your local output, it is here despite it being the second day of the week.


Hi compo,

I've tried this and it works great!
could I also echo the day in the cmd box like Echo %%A as "dd" or something ?

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Check day and if day = .. start file

#6 Post by Samir » 19 Jul 2017 06:19

malortje wrote:
Compo wrote:You could simplify the entire process by just using the built-in WMIC.exe.

Code: Select all

For /F EOL^=D %%A In ('WMIC Path Win32_LocalTime Get DayOfWeek'
) Do If %%A Equ 1 Start "" "Your Program.exe"
You just need to verify that Monday is allocated 1 in your local output, it is here despite it being the second day of the week.


Hi compo,

I've tried this and it works great!
could I also echo the day in the cmd box like Echo %%A as "dd" or something ?
You can, but since %%A is just going to be a number, you will need a way to convert that to a day of the week if that's what you want to see.

Foxman1999
Posts: 4
Joined: 28 Jul 2017 09:17

Re: Check day and if day = .. start file

#7 Post by Foxman1999 » 28 Jul 2017 11:15

If you have the long date set up on your PC to display the day of the week, you can use something like the following to display the day of the week.

Code: Select all

echo %date:~0,3%


This would display the first character, and the next three characters after it.

Post Reply