@echo off & setlocal
:: ---------------------
::
:: Adaptation of DOS script to select a random file from a folder, and copy it to a destination
::
:: Doug Sharp - 2015 for surfaceforums.net
::
:: Specify paths from the user folder downwards.
:: Example: c:\Users\Doug\Onedrive\ is "\Onedrive\"
::
:: ---------------------
set "ImageDir=\OneDrive\Pictures\Wallpaper Surface\"
set "HomePageDir=\OneDrive\HomePage\"
set SearchFor=*.jpg
::
set "Here=%USERPROFILE%%ImageDir%"
set "There=%USERPROFILE%%HomePageDir%"
@set /a "rdm=%random%"
set /a "rdm=%random%"
::Push to your path.
pushd "%Here%"
::Count all files in your path.
set /a "counter=0"
for /f "delims=" %%i in ('dir %SearchFor% /b ^|find "."') do call :sub1
::This function gives a value from 1 to upper bound of files
set /a "rdNum=(%rdm%*%counter%/32767)+1"
::Select a random file
set /a "counter=0"
for /f "delims=" %%i in ('dir %SearchFor% /b ^|find "."') do set "fileName=%%i" &call :sub2
::Pop back from your path.
popd "%Here%"
goto :eof
:: end of main
:: start of sub1
:sub1
::For each found file set counter + 1.
set /a "counter+=1"
goto :eof
:: end of sub1
:: start of sub2
:sub2
::1st: count again,
::2nd: if counted number equals random number then copy the file.
set /a "counter+=1"
if %counter%==%rdNum% copy /b/v/y "%Here%%fileName%" "%There%MyHomePageBackground.jpg"
goto :eof
exit