This question was posed as an answer to this thread where running FOR
loops in batch with UAC elevation code generates voluminous errors involving non-existent directories.
Here is a screenshot of such:
The problem is the erroneous insertion of Local
and repeated Application Data
folder names. These error messages do not appear when not using the UAC elevation code.
Is it possible that something in WScript is invoking the wrong environment settings for HOMEPATH
on the WshEnvironment
object?
It might relate more to the upgrade (years ago) of XP to Win 7 HP, where for some reason, the ghosts of Documents and Settings
, Local Settings
, and Application Data
won't go away. Simply putting in commands such as
set LOCALAPPDATA=C:\Users\%username%\AppData\Local
set APPDATA=C:\Users\%username%\AppData\Roaming
doesn't change anything. With admin privileges %LocalAppData%
doesn't seem right in batch.
Searching the registry brings up virtually nix for things like Application Data
so the source is yet to be determined. Perhaps some other facet of the Shell Object?
Here's the script + copy % paste
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights (thanks to TanisDLJ at Stackoverflow)
:::::::::::::::::::::::::::::::::::::::::
@echo off
color 1E
mode 100,50
VERIFY > nul
CLS
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo args = "" >> "%temp%\getadmin.vbs"
echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
echo args = args ^& strArg ^& " " >> "%temp%\getadmin.vbs"
echo Next >> "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", args, "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" %*
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0"
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::
:: Automatically check & get admin rights (thanks to TanisDLJ at Stackoverflow)
:::::::::::::::::::::::::::::::::::::::::
@echo off
color 1E
mode 100,50
VERIFY > nul
CLS
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\icacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo args = "" >> "%temp%\getadmin.vbs"
echo For Each strArg in WScript.Arguments >> "%temp%\getadmin.vbs"
echo args = args ^& strArg ^& " " >> "%temp%\getadmin.vbs"
echo Next >> "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", args, "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs" %*
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0"
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0"
set CURRDRIVE=C
FOR /F "usebackq delims==" %%G IN (`dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "myString"`) DO (set "foundMyString=%%~pG")
pause
Note that on this machine the spam doesn't display if not running the UAC code (all above the "START") and appears to work fine in both cases. Copy the code below the "START" with "%" replacing "%%" in an elevated command prompt and still get the same messages.
No comments:
Post a Comment