2

I'm trying to force Windows 10 to change the logon/lockscreen pictures every so often with our own company ones (corporate branding).

The following directory contains the Microsoft stock images:

C:\Windows\Web\Screen
img100.jpg
img101.png
img102.jpg
img103.png
img104.jpg
img105.jpg

But simply replacing those won't result in the default lockscreen changing. You first need to force delete the contents in this directory which contains the last lockscreen image that was copied over from the location above: C:\ProgramData\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z

I do this by performing the following:

robocopy.exe /purge C:\tmp\empty C:\ProgramData\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z

I have my own company corporate images in the following location with the same filenames:

C:\Windows\Backgrounds\Lockscreen
img100.jpg
img101.png
img102.jpg
img103.png
img104.jpg
img105.jpg

My plan is to have a scheduled task that purges the contents of:

C:\Windows\Web\Screen

Then purges the contents of:

C:\ProgramData\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z

Then I have the following script to randomly select a file and copy it over:

@echo off
setlocal EnableDelayedExpansion
cd C:\Windows\Backgrounds\Lockscreen
set n=0
for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" C:\Windows\Web\Screen

The problem i have, is that no matter how many times i run the above, it always picks the same file to copy over and over. I don't know what changes need to be made for it to truly work at random? I've also noticed, that the custom lockscreen only comes into effect if the copied file is renamed to img100.jpg (seems to only want to accept that filename - not sure why). I'm not sure how to incorporate the renaming of the copied file to img100.jpg into the script either.

Any ideas?

3
  • As a small comment regarding randomization, the batch script works in practice (assuming there are files in the folder you are cycling through). But there is nothing stopping that batch file from producing long strings of the same integer(s). Jul 6, 2020 at 5:05
  • Thanks for your comment Anaksunaman. I'm hoping someone can help modify the script so it produces a log remembering the last file copied, then chooses to exclude that file in the next random selection process.
    – Mastaxx
    Jul 8, 2020 at 14:48
  • May be this task will more easily if you get the register key for your wallpaper to by set, and not by copy task. So, only by random file to pic, and add/replace the correct key value.
    – Io-oI
    Jul 11, 2020 at 23:54

1 Answer 1

0
@echo off

setlocal EnableDelayedExpansion
cd /d "C:\Windows\Backgrounds\Lockscreen"

for /f %%i in ('dir *.* /b ^| find /v /c ""
')do set /a _rnd=!random!*(10%%i-100)/32768+100
copy /y *!_rnd!.* C:\Windows\Web\Screen & endlocal

This is something that works in my tests considering:

  1. The folder has only those files listed in your question:
   C:\Windows\Backgrounds\Lockscreen\img100.jpg
   C:\Windows\Backgrounds\Lockscreen\img101.png
   C:\Windows\Backgrounds\Lockscreen\img102.jpg
   C:\Windows\Backgrounds\Lockscreen\img103.png
   C:\Windows\Backgrounds\Lockscreen\img104.jpg
   C:\Windows\Backgrounds\Lockscreen\img105.jpg
  1. Your file name layout is in the range 100 - 105

  • To copy the file also renaming to the destination:
copy /y "*!_rnd!.*" C:\Windows\Web\Screen\img100.jpg & endlocal

  • To restrict to *.png&*.jpg files only:
@echo off

setlocal EnableDelayedExpansion
cd /d "C:\Windows\Backgrounds\Lockscreen"

for /f %%i in ('dir /b *.png *.jpg ^| find /v /c ""
')do set /a _rnd=!random! * (10%%i - 100) / 32768 + 100
copy /y /v "*!_rnd!.*" C:\Windows\Web\Screen\img100.jpg & endlocal

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .