Skip to content

Instantly share code, notes, and snippets.

@KenDB3
Last active July 30, 2017 14:57
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 KenDB3/81b55f829d0a45dc084a6adc6e306675 to your computer and use it in GitHub Desktop.
Save KenDB3/81b55f829d0a45dc084a6adc6e306675 to your computer and use it in GitHub Desktop.
@echo off
REM This script was built for a friend who needed to have a folder full of possible ANSI Welcome screens
REM named in a pattern of: welcome.1, welcome.2, welcome.3, etc....
REM The script will build an array of the files, get a count of how many are in the folder, and randomly
REM select a file to be converted to "welcome.clr".
REM Much google searching and reading through Stack Overflow was done, but the method works pretty well.
REM !!CHANGE THIS to your directory where the Welcome screens are stored!!
REM Quotes are needed if the folder path has spaces in it.
set WELCOMEFOLDER="c:\Temp\example\"
REM make sure this includes the trailing backslash at the end of the directory.
REM !!CHANGE THIS to the directory where you want the randomly chosen Welcome screen to be stored.
set OUTPUTPATH="c:\Temp\"
REM make sure this includes the trailing backslash at the end of the directory.
REM --------No Need to Edit Below This Line--------
REM Change Directory to Start Folder
@echo on
cd %WELCOMEFOLDER%
@echo off
REM All welcome screen files should be added to a single folder with no other files
REM at all in that folder (basically any file in that folder could be used for the random screen).
REM Welcome screen files are assumed to be named in a pattern where the
REM file name is "welcome" and the file extension is "1" for the first screen
REM and then each additional welcome screen file is numbered sequentially with no
REM breaks in the pattern. Don't use leading zeros (0).
REM An sample of the pattern would look like this:
REM welcome.1, welcome.2, ... , welcome.9, welcome.10, welcome.11, ... , welcome.99, welcome.100, etc...
REM where elipses indicate skipping ahead in the pattern.
REM First, I want to introduce a variable that messes with when the random numbers get generated.
REM Basically CMD suffers from backwards DOS compatibility in batch files where the Random number
REM generator is "seeded" by the time. So, if this script runs at the same time every day, it
REM increases the likelyhood that things will be less random. If you PING something, whether it be an
REM internal or external IP, it should create some variation in the timing of the script.
REM !!FEEL FREE TO CHANGE THIS TO AN INTERNAL IP, otherwise it will PING 8.8.8.8 which is
REM a DNS server maintained by Google.
REM Comment the ping section out if you don't need this (example this script runs after an event,
REM not based on a particular time schedule).
echo Pinging Google's DNS server 5 times to get some randomness
echo -------------------------------------------------------------------------------
PING 8.8.8.8 -n 5
REM Next, offset randomness with Magic ... no seriously this is magic and I can't explain it.
REM If I run the random number generator twice (or more) in a batch file, everything
REM after the first run turns into something MORE random somehow. I discovered this by
REM playing with the random variable and testing different ways of doing the math and then
REM comparing answers. It was purely chance that this pattern appeared.
set /a randmeasure=%random%
echo -------------------------------------------------------------------------------
echo Your (not so random) Random Number Seed for the start of this run is %randmeasure%
REM A major part of the solution is from here:
REM http://stackoverflow.com/questions/19540089/how-to-get-the-list-of-filenames-in-a-directory-and-store-that-in-a-variable-usi
REM According to the above link, turning off DelayedExpansion will keep
REM you from running into problems if the file contains an exclamation mark (!).
REM However, if you are using the right pattern (welcome.1, welcome.2, etc) this
REM should not be a problem. But... just in case :-)
setlocal disableDelayedExpansion
:: Load the file path "array"
for /f "tokens=1* delims=:" %%A in ('dir /a:-d /b^|findstr /n "^"') do (
set "file.%%A=%%B"
set "file.count=%%A"
)
REM List how many files we found
echo %file.count% files were found in %WELCOMEFOLDER%
echo The file list is:
:: Access and display all the values (and turn DelayedExpansion back on)
setlocal enableDelayedExpansion
for /l %%N in (1 1 %file.count%) do echo !file.%%N!
REM This part is modified from some code found here:
REM http://stackoverflow.com/questions/5777400/how-to-use-random-in-batch-script
REM It will produce number between 1 to whatever %file.count% is.
set /a randseed=%random%
echo Your (better) Random Number Seed for the end of this run is %randseed%
set /a randwelcome=%randseed% * %file.count% / 32768 + 1
echo Your random number is %randwelcome%
echo !file.%randwelcome%! (in %WELCOMEFOLDER%) becomes welcome.clr (in %OUTPUTPATH%)
COPY /Y welcome.%randwelcome% %OUTPUTPATH%welcome.clr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment