Created
April 15, 2020 05:10
-
-
Save mark05e/930fd43ecd4058d6aa94721941a36d8a to your computer and use it in GitHub Desktop.
My Windows batch file boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: ----------------------------------------------------------------------------- | |
:: Name: MyBatchScriptBoilerplate.cmd | |
:: Purpose: A template for future bash scripts | |
:: Author: mark@XXXX | |
:: Revision: March 2013 - initial version | |
:: April 2013 - added support for FooBar v2 switches | |
:: ----------------------------------------------------------------------------- | |
@ECHO OFF | |
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION | |
SET me=%~n0 | |
SET parent=%~dp0 | |
SET logpath=%~dp0\logs | |
:: Check Pause before exit | |
SET PauseOnExit=0 | |
:: Generate Timestamp | |
FOR /f "delims=" %%a IN ('wmic OS Get localdatetime ^| find "."') DO SET DateTime=%%a | |
SET Yr=%DateTime:~0,4%&& SET Mon=%DateTime:~4,2%&& SET Day=%DateTime:~6,2% | |
SET Hr=%DateTime:~8,2%&& SET Min=%DateTime:~10,2%&& SET Sec=%DateTime:~12,2% | |
SET datefmt1=%Yr%-%Mon%-%Day%_%Hr%-%Min%-%Sec% | |
SET datefmt2=%Yr%-%Mon%-%Day%_%Hr%-%Min% | |
SET datefmt3=%Yr%%Mon%%Day%_%Hr%%Min% | |
:: Create a log file | |
IF NOT EXIST "%logpath%" MKDIR %logpath% | |
SET log=%~dp0\logs\%me%.%datefmt3%.txt | |
:: ----------------------------------------------------------------------------- | |
:: DO WORK - START | |
:: This is where you need to add your code | |
:: ----------------------------------------------------------------------------- | |
ECHO do work - starting @ %date% %time% | |
ECHO Script Name: %me% | |
ECHO Folder Path: %parent% | |
ECHO Username: %USERNAME% | |
ECHO Hostname: %HOSTNAME% | |
ECHO Logged In Users: && QUERY USER | |
:: do something cool, then log it | |
CALL :tee "%me%: Hello, world!" | |
:: sleep for 5 seconds | |
PING.EXE -n 5 127.0.0.1 > NUL | |
:: ----------------------------------------------------------------------------- | |
:: DO WORK - END | |
:: :: Check Pause before exit | |
IF "%PauseOnExit%"=="0" PAUSE | |
:: :: force execution to quit at the end of the "main" logic | |
EXIT /B %ERRORLEVEL% | |
:: ----------------------------------------------------------------------------- | |
:: a function to write to a log file and write to stdout | |
:tee | |
ECHO %* >> "%log%" | |
ECHO %* | |
EXIT /B 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment