Page 1 of 1

Check day and if day = .. start file

Posted: 14 Jul 2017 04:26
by malortje
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!

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

Posted: 15 Jul 2017 00:15
by pieh-ejdsch
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

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

Posted: 15 Jul 2017 04:39
by Compo
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.

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

Posted: 15 Jul 2017 07:37
by aGerman
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

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

Posted: 17 Jul 2017 05:31
by malortje
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 ?

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

Posted: 19 Jul 2017 06:19
by Samir
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.

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

Posted: 28 Jul 2017 11:15
by Foxman1999
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.