pafflick
pafflick

Reputation: 17

Pick specific result from REG QUERY search

I'm trying to extract the Windows 10 Logon background image (from "Windows Spotlight") and copy it into another location for further use. So far I've successfully done it using the batch code provided below.

@echo off
for /F "skip=6 tokens=2*" %%A in ('REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Creative\ /F landscapeImage /S /V') DO (
  for %%F in (%%B) do (
    set Background=%%F
    goto :next
  )
)
:next
copy %Background% "d:\background.jpg" /Y

My biggest concern is that although it worked well on my machine, the registry structure might look differently on other computers and my code is totally unprepared for that.

The reason for this might be the blind guess with the skip=6 parameter. The search in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Creative\ for landscapeImage returned 3 results and only the last one leads to the image that I'm seeing on the logon page. Since I didn't know how to pick specific search result, I used the skip parameter, but for this reason, the batch script will work properly only if there are at least 3 search results and the 3rd one is the one I'm actually looking for - in other instances it will simply fail.

To summarize, I don't know how to:

  1. Check how many search results are returned from the REG QUERY command.

  2. Pick a specific search result.

Another concern (unrelated to the batch script issue described above) is that I'm not sure how Windows decides which image is used as the background (it changes regularly). I just assumed that it's the last one from the search results (it was true in my case). I couldn't find any other mentions of the background image file within the registry. I tried looking for other parameters from that key, but that didn't return any results either. Any suggestions for this problem are also welcome.

This is the best I could come up with, but now I just simply got stuck.

Upvotes: 0

Views: 330

Answers (2)

pafflick
pafflick

Reputation: 17

For anyone interested, here's the final batch code I use to extract the Windows 10 Logon background image (based on Squashman's answer):

@echo off
set "query=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Creative\"
for /F "TOKENS=1-2* delims= " %%G in (
    'REG QUERY %query% /F landscapeImage /S /V ^|find /I "landscapeImage"'
) DO (
    set "image=%%I"
)
copy %image% "d:\background.jpg" /Y

I put a shortcut to the batch file in %AppData%\Microsoft\Windows\Start Menu\Programs\Startup to make a copy of the background image (from the so-called "Windows Spotlight") in the desired location on each system startup, so that the image stays "up-to-date".

Upvotes: 0

Squashman
Squashman

Reputation: 14290

You could consider assigning all the query output to separate variables. An array if you want to call it that. This will assign all the images to an array of variables and give you a count of how many it found.

@echo off
set "cnt=0"
set "query=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\LogonUI\Creative\"
for /F "TOKENS=1-2* delims= " %%G in (
    'REG QUERY %query% /F landscapeImage /S /V ^|find /I "landscapeImage"'
) DO (
    set /a "cnt+=1"
    CALL set "image%%cnt%%=%%I"
)
echo Number of Images=%cnt%
set image
pause

Note: On my computer, it also output 3 images and the last one is the current logon background image.

If you just want to set the last image to a variable just ditch the code that does the counting and use a basic set command.

set "image=%%I"

My logon background image changed again and the third image from the query is the logon screen image. Seems to be consistent.

Upvotes: 2

Related Questions