1

I do not have access to Windows task scheduler on my machine as this is restricted by our IT. I was hoping to get around this by placing a batch file in my startup folder that runs on startup. I only need the batch file to run every 30 days, preferably on the 15th of the month.

Does anybody know how I'd be able to make this happen?

For reference my batch file at the moment just runs 1 executable which does the entire work.

Mofi
  • 46,139
  • 17
  • 80
  • 143
Dunuts
  • 159
  • 1
  • 8

2 Answers2

0

You could run this batchfile from startup. It will run every time startup apps are run. That means it might run several times on the 15th day of the month. And, it will not run on the 15th if startup apps are not run on the 15th.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "usebackq tokens=*" %%a IN (`wmic os get localdatetime`) DO (
    SET "S=%%a"
    IF "!S:~6,2!" EQU "15" (
        ECHO RUN PROGRAM HERE
    )
)
lit
  • 14,456
  • 10
  • 65
  • 119
  • Can I set it to only run once on the 15th? As the program takes about 2 hours to run and only needs to be run once. – Dunuts Jun 19 '17 at 15:57
  • There is no control in this code to prevent multiple runs. There would need to be something that the code could see that would indicate that it has already run for this date. Perhaps a file? – lit Jun 19 '17 at 16:05
0

Here is my solution for a task scheduling without using Windows task scheduler:

@echo off
rem Get local date and time in a region independent format.
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDateTime=%%I"

rem Is the day in month 01 to 14, remove the system attribute from
rem batch file and exit the batch file execution as nothing to do.
rem This is a string comparison and not an integer comparison.
if "%LocalDateTime:~6,2%" LSS "15" %SystemRoot%\System32\attrib.exe -s "%~f0" & goto :EOF

rem Get attributes of the batch file.
set "BatchAttributes=%~a0"

rem Is the system attribute set, do nothing and exit the batch file.
if "%BatchAttributes:~4,1%" NEQ "-" goto :EOF

rem Set system attribute on the batch file.
%SystemRoot%\System32\attrib.exe +s "%~f0"

rem Add here the commands to execute once in month on 15th or any later day.
echo Run the application ...

This batch file is written to run the command lines at bottom on 15th day in month exactly one times. But in case of the batch file is not started on 15th day (Sunday, vacation, ...), the commands at bottom are executed on next day in second half of month on which the batch file is executed manually or on startup of Windows.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • attrib /?
  • call /? ... explains %~a0 (batch file attributes) and %~f0 (batch file name with extension and full path).
  • echo /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • wmic /?
  • wmic os /?
  • wmic os get /?
  • wmic os get localdatetime /?

And read also Single line with multiple commands using Windows batch file.

Mofi
  • 46,139
  • 17
  • 80
  • 143