Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kodybrown
Created October 9, 2019 03:09
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 kodybrown/b703b778b75b219a29356082d6f8cde6 to your computer and use it in GitHub Desktop.
Save kodybrown/b703b778b75b219a29356082d6f8cde6 to your computer and use it in GitHub Desktop.
Launch random .URL file
@setlocal EnableDelayedExpansion
@echo off
set "SourceDir=."
REM count the number of files
pushd "!SourceDir!"
set /a FileCount=0
for /f "usebackq delims=|" %%I in (`dir /a-d-s-h /b *.url`) do set /a FileCount+=1
popd
REM echo .url file count = !FileCount!
REM pick a random number
call :GetRandumNumber !FileCount!
REM echo random number = !RandomNumber!
REM pick the file
pushd "!SourceDir!"
set /a FileCount=0
for /f "usebackq delims=|" %%I in (`dir /a-d-s-h /b *.url`) do (
set /a FileCount+=1
REM echo current file # = !FileCount!
REM echo random file = %%I
REM if !FileCount! equ !RandomNumber! echo launching: %%I & start "" "%%I"
if !FileCount! equ !RandomNumber! echo launching: %%I
)
popd
@endlocal & exit /b
:GetRandumNumber MAX
REM for some reason this doesn't seem to work, unless I call it twice..
set /a RandomNumber=(%RANDOM% * 100) / 32768 + 1
set /a RandomNumber=(%RANDOM% * %~1) / 32768 + 1
goto :eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment