Skip to content

Instantly share code, notes, and snippets.

@samhattangady
Created October 15, 2023 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhattangady/8d160a591d485dd5a5759749f254fd9d to your computer and use it in GitHub Desktop.
Save samhattangady/8d160a591d485dd5a5759749f254fd9d to your computer and use it in GitHub Desktop.
Some helpful batch scripts

Just a note of some of the batch scripts I have for various purposes

timecmd

Similar to time on linux. Used to measure how long a command takes to run. Source: https://stackoverflow.com/a/6209392/5453127

@echo off
@setlocal

set start=%time%

:: Runs your command
cmd /c %*

set end=%time%
set options="tokens=1-4 delims=:.,"
for /f %options% %%a in ("%start%") do set start_h=%%a&set /a start_m=100%%b %% 100&set /a start_s=100%%c %% 100&set /a start_ms=100%%d %% 100
for /f %options% %%a in ("%end%") do set end_h=%%a&set /a end_m=100%%b %% 100&set /a end_s=100%%c %% 100&set /a end_ms=100%%d %% 100

set /a hours=%end_h%-%start_h%
set /a mins=%end_m%-%start_m%
set /a secs=%end_s%-%start_s%
set /a ms=%end_ms%-%start_ms%
if %ms% lss 0 set /a secs = %secs% - 1 & set /a ms = 100%ms%
if %secs% lss 0 set /a mins = %mins% - 1 & set /a secs = 60%secs%
if %mins% lss 0 set /a hours = %hours% - 1 & set /a mins = 60%mins%
if %hours% lss 0 set /a hours = 24%hours%
if 1%ms% lss 100 set ms=0%ms%

:: Mission accomplished
set /a totalsecs = %hours%*3600 + %mins%*60 + %secs%
echo command took %totalsecs%.%ms%s

playsound

To play a sound using vlc (headless) from commandline.

I use it to check if compilations have run successfully etc.

@echo off
call "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" -I dummy --dummy-quiet --play-and-exit %*

Example usage:

@echo off
call timecmd zig build -Dbuild_mode=hotreload %*
if errorlevel 1 goto :compilefail

call playsound local\audio\compile_success.mp3
goto :done

:compilefail
call playsound local\audio\compile_fail.mp3
goto :done

:done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment