@echo off

rem Created for Windows 10 AME, tested on version 20H2
rem Author: Logan Darklock <logandarklock@gmail.com> (no spam please)
rem Co-author: lucid

cls
echo  :: Windows 10 AME Lockscreen Wallpaper Changer
echo.
echo     Working...
echo.

setlocal
set SCRIPT=%0
set IMAGE=%1

if "%IMAGE%" == "" (
	cls
	echo  :: Windows 10 AME Lockscreen Wallpaper Changer
	echo.
	echo     To change the lockscreen wallpaper, drag-and-drop
	echo     an image file onto the script icon.
	goto pause
)

rem Verify image extension, per lucid's request
set NAME=%IMAGE:"=%

rem Convert to lowercase
set NAME=%NAME:A=a%
set NAME=%NAME:B=b%
set NAME=%NAME:C=c%
set NAME=%NAME:D=d%
set NAME=%NAME:E=e%
set NAME=%NAME:F=f%
set NAME=%NAME:G=g%
set NAME=%NAME:H=h%
set NAME=%NAME:I=i%
set NAME=%NAME:J=j%
set NAME=%NAME:K=k%
set NAME=%NAME:L=l%
set NAME=%NAME:M=m%
set NAME=%NAME:N=n%
set NAME=%NAME:O=o%
set NAME=%NAME:P=p%
set NAME=%NAME:Q=q%
set NAME=%NAME:R=r%
set NAME=%NAME:S=s%
set NAME=%NAME:T=t%
set NAME=%NAME:U=u%
set NAME=%NAME:V=v%
set NAME=%NAME:W=w%
set NAME=%NAME:X=x%
set NAME=%NAME:Y=y%
set NAME=%NAME:Z=z%

if "%NAME:~-4%" == ".jpg"  (goto validextension)
if "%NAME:~-5%" == ".jpeg" (goto validextension)
if "%NAME:~-4%" == ".png"  (goto validextension)
if "%NAME:~-4%" == ".gif"  (goto validextension)
if "%NAME:~-5%" == ".webp" (goto validextension)
if "%NAME:~-4%" == ".bmp"  (goto validextension)

cls
echo  :: Windows 10 AME Lockscreen Wallpaper Changer
echo.
echo     Invalid image format!
echo.
echo     Supported image formats: .jpg, .jpeg, .png, .gif, .webp, .bmp
goto pause

:validextension

timeout /t 1 /nobreak > NUL
openfiles > NUL 2>&1

if not %errorlevel% == 0 (
	cls
	echo  :: Windows 10 AME Lockscreen Wallpaper Changer
	echo.
	echo     Elevating to administrator...
	powershell "Start-Process """%SCRIPT:"=`"""%""" -ArgumentList """%IMAGE:"=`"""%""" -Verb RunAs"
	goto end
)

rem Copy wallpaper to the right spot
takeown /f "%WINDIR%\Web\Screen\img100.jpg" >nul
takeown /f "%WINDIR%\Web\Screen\img103.png" >nul
takeown /f "%WINDIR%\Web\Wallpaper\Windows\img0.jpg" >nul
icacls "%WINDIR%\Web\Screen\img100.jpg" /reset >nul
icacls "%WINDIR%\Web\Screen\img103.png" /reset >nul
icacls "%WINDIR%\Web\Wallpaper\Windows\img0.jpg" /reset >nul
copy "%IMAGE%" "%WINDIR%\Web\Screen\img100.jpg" /y >nul
copy "%IMAGE%" "%WINDIR%\Web\Screen\img103.png" /y >nul
copy "%IMAGE%" "%WINDIR%\Web\Wallpaper\Windows\img0.jpg" /y >nul

rem Clear cache
takeown /f "%PROGRAMDATA%\Microsoft\Windows\SystemData" >nul
icacls "%PROGRAMDATA%\Microsoft\Windows\SystemData" /reset >nul
takeown /f "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18" >nul
icacls "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18" /reset >nul
takeown /f "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly" >nul
icacls "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly" /reset >nul
takeown /f "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z" >nul
icacls "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z" /reset >nul
takeown /f "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z\*" >nul
icacls "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z\*" /reset >nul
del /q /f "%PROGRAMDATA%\Microsoft\Windows\SystemData\S-1-5-18\ReadOnly\LockScreen_Z\*" >nul

cls
echo  :: Windows 10 AME Lockscreen Wallpaper Changer
echo.
echo     Wallpaper successfully changed! You may have to reboot
echo     for it to take effect.

:pause
echo.
pause

:end