I have a startup batch file. I want it to be able to scan a folder (c:\bmp) and choose a random .bmp file in that folder then set it as the wallpaper in windows
(or at least set it as %userprofile%\Application Data\Microsoft\Internet Explorer\Internet Explorer Wallpaper.bmp).
I don't know any commands that will pick random vaues so I need help doing this.
cmd, vbs, ps, bash
autoit, python, swift
#2 17 Feb 2007 11:15
I don't know of a way to set an image as wallpaper solely using a batch script (though I did once find a utility that would do it many moons ago), but I do know how to do the rest:
Code: Select all
@echo off setlocal enabledelayedexpansion set folder=c:\bmp set target=%userprofile%\Application Data\Microsoft\Internet Explorer\Internet Explorer Wallpaper.bmp set count=0 set x=0 REM put all the files into a pseudo-array prefixed with "PIC_" for /r "%folder%" %%a in (*.bmp) do ( set PIC_!count!=%%~a set /a count+=1 ) REM Use the 'modulo' function to get a usable value from system variable %random% set /a x="%random% %% count" REM Pull the relevant item out of the "PIC_" 'array' set chosen=!PIC_%x%! echo:I chose :: %chosen% copy /y "%chosen%" "%target%" 1>nul pause & REM remove this whole line if you don't want the window to stay open at the end
#3 18 Feb 2007 10:50
This is a great script. I never knew what a pseudo-array was, but now I shall be using those. I see the logic of how your code works by counting the total value of bmp files then using the variable %random% to get a random number then use the corresponding number. Quite a way of thinking, more complicated than I thought. Thanks again!
To set the wallpaper - for those who might be interested!!!:rolleyes: Only if you use irfanview!
i_view32.exe "%chosen%" /wall=0 /killmesoftly
Open "%chosen%" and set is as wallpaper (centered).
i_view32.exe "%chosen%" /wall=1 /killmesoftly
Open "%chosen%" and set is as wallpaper (tiled).
i_view32.exe "%chosen%" /wall=2 /killmesoftly
Open "%chosen%" and set is as wallpaper (stretched).
and sorry if this is a bit off topic as this is a .vbs script I found which can do the same thing just now.
WallpaperFolder = "C:\bmp"
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.Namespace(WallpaperFolder)
set files = objFolder.Items
command = """%ProgramFiles%\IrfanView\i_view32.exe"" """ & files.Item(cint((files.Count - 1) * Rnd)).Path & """ /wall=2 /killmesoftly"
set WshShell = CreateObject("WScript.Shell")
WshShell.Run command, 0, true
Last edited by NDog (18 Feb 2007 11:18)
#4 03 Dec 2013 21:35
(answer, found at stackoverflow.com/a/20361034/2165415)
here is a CMD code, which outputs the random file name (customize it to your needs):
Code: Select all
@echo off & setlocal set "workDir=C:\source\folder" rem Read the %random%, two times is'nt a mistake! Why? Ask Bill. rem In fact at the first time %random% is nearly the same. @set /a "rdm=%random%" set /a "rdm=%random%" rem Push to your path. pushd "%workDir%" rem Count all files in your path. (dir with /b shows only the filenames) set /a "counter=0" for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1 rem This function gives a value from 1 to upper bound of files set /a "rdNum=(%rdm%*%counter%/32767)+1" rem Start a random file set /a "counter=0" for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2 rem Pop back from your path. popd "%workDir%" goto :eof :: end of main :: start of sub1 :sub1 rem For each found file set counter + 1. set /a "counter+=1" goto :eof :: end of sub1 :: start of sub2 :sub2 rem 1st: count again, rem 2nd: if counted number equals random number then start the file. set /a "counter+=1" if %counter%==%rdNum% ( :: OUTPUT ALERT BOX with FILENAME MSG * "%fileName%" ) goto :eof :: end of sub2 :: -snap--- end of batch
#5 06 Dec 2013 15:24
Only 7 years late to the party
cmd | *sh | ruby | chef
#6 12 Jun 2021 19:03
@bluesxman It's been 13 years and your answer is still helping people!
Used this for a twitch streamer who wants to pop a random image on the screen with a voice command.
#7 14 Jun 2021 11:03
I probably should have had a caveat that it might take a long time if there is a large volume of files and that RANDOM has an upper bound of 32768 (if memory serves me correctly) so this would be a hard limit on the files it will consider. But as per previous caveat, it would take a while to load them all anyway.
Good to know something I did 13 years ago is still in use