Morning Start Up batch file


The following is a batch file that I often re-create as I start working in a new environment. It just starts up a set of programs that I usually want to have executing at the start of my workday. I customize it as time goes by, new program versions emerge, and jobs come and go.

:: Syntax for starting programs/batch files
::   CALL :{startoption} {prog} {dir} [{param}]...
::   where {startoption} is one of:
::                       startprog      = Start the process if it is not already running.
::                       startoncetoday = Start the process if the initiating program or
::                                        batch file has not been accessed today.
::         {prog}  = The program (name and extension) or batch file to start.
::         {dir}   = If {dir} ends with a backslash, then {dir} is the path of the folder
::                     containing {prog}.
::                   Otherwise, {dir} is the path to the program or batch file which will
::                     initiate {prog}.
::         {param} = Up to 6 optional parameter values.
@SETLOCAL
@SET _log=C:\Utilities\startday.log
@ECHO %DATE% %TIME% > %_log%
@CALL :startprog "Ssms.exe" "C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\"^
                 "C:\***MyUtilityScript***" "C:\***AnotherUtilityScript***"^
                 -S "***server*address***"
@CALL :startprog "chrome.exe" "C:\Program Files\Google\Chrome\Application\"
@CALL :startprog "OUTLOOK.EXE" "C:\Program Files (x86)\Microsoft Office\root\Office16\"
@CALL :startoncetoday "dailyCopy.bat" "C:\Utilities\"
@CALL :startprog "EXCEL.EXE" "C:\Program Files (x86)\Microsoft Office\root\Office16\" /e "M:\Links.xlsx"
@CALL :startprog "Time.exe" "explorer.exe" "shell:Appsfolder\Microsoft.WindowsAlarms_8wekyb3d8bbwe!App"
@ENDLOCAL
@EXIT /B

:startprog [1:prog] [2:dir] [3:p1] [4:p2] [5:p3] [6:p4]
@  CALL :setpath "%~1" "%~2"
@TASKLIST /FI "IMAGENAME eq %~1" 2>NUL | FIND /I /N "%~1">NUL
@IF "%ERRORLEVEL%" == "0" (
@  ECHO [%~1] is already running >> %_log%
) ELSE (
@  ECHO starting [%~1] >> %_log%
@  START "" "%_path%" %~3 %~4 %~5 %~6 >> %_log%
)
@EXIT /B

:startoncetoday [1:prog] [2:dir] [3:p1] [4:p2] [5:p3] [6:p4]
@CALL :setpath %~1 %~2
@SET _path=%_path:\=\\%
@SET _macro=FOR /F "skip=2" %%D IN ('wmic DataFile where "Name='%%_path%%'" get LastAccessed /value') DO @CALL :getLastAccessed %%D
@%_macro%
@IF [%_LastAccessed%] == [%DATE%] (
@  ECHO [%~1] has already run today >> %_log%
@  (SET _LastAccessed=)
) ELSE (
@  CALL :startprog %~1 %~2 %~3 %~4 %~5 %~6
)
@EXIT /B

:getLastAccessed [1:wmicOutput]
@SET _d=%~2
@IF [%1] == [LastAccessed] SET _LastAccessed=%_d:~0,4%-%_d:~4,2%-%_d:~6,2%
@EXIT /B

:setpath [1:prog] [2:dir]
@SET _path=%~2%
@IF [%_path:~-1%] == [\] SET _path=%~2%~1
@EXIT /B

One item to note is that the :startoncetoday routine is able to work from the file’s access date (not it’s modified date). This was not easy. I scoured the net and had to cobble together a solution from bits and pieces here and there, so I really wanted to make sure that I didn’t lose this tidbit. Hence this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s