2

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:

enter image description here

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.

  • 2
    Could you edit your question to include the batch script you're using? – Vinayak Dec 6 '14 at 11:58
  • Do I correctly assume you're encountering this problem on a Windows 7 machine? – I say Reinstate Monica Dec 6 '14 at 13:05
  • @Vinayak:Question edited. @ Twisty: Windows 7 Home Premium upgraded from XP. – Laurie Stearn Dec 6 '14 at 21:43
  • Ok, I'll take a look. FYI, Stack Exchange sites don't let you use @ to notify more than one person per comment, and the first use of @ wins. – I say Reinstate Monica Dec 7 '14 at 3:02
0

EDIT: Answer by Frederik Long from TechNet Forums

The problem is linked to a recursive junction point resulting form upgrading to Windows 7 from Windows XP. The fix is to run the following command:

rd "%SYSTEMDRIVE%\Documents and Settings"

It removes the junction point without deleting any files.


It appears that you've used the UAC elevation code twice in your script and that there's a redundant Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0" although I'm not sure why you have aPUSHD without a corresponding POPD.

Other than that I don't really see much of a problem. I modified your script to include echoing of %%~pG which I believe matches paths that either contain myString or paths that have sub-directories with myString in their folder names.

I changed

FOR /F "usebackq delims==" %%G IN (`dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "myString"`) DO (set "foundMyString=%%~pG")

to

FOR /F "usebackq delims==" %%G IN (`dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "Macromedia"`) DO (set "foundMyString=%%~pG" & echo %%~pG)

and executed the sciript with CURRDRIVE set to C and I didn't run into any problems like repetition of %LOCALAPPDATA% or %APPDATA% paths. Here's what I got:

\Users\Vinayak\AppData\Local\Macromedia\
\Users\Vinayak\AppData\Roaming\
\Users\Vinayak\AppData\Roaming\Macromedia\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\#Security\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\macromedia.com\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\macromedia.com\support\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\macromedia.com\support\flashplayer\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\www.macromedia.com\
\Users\Vinayak\AppData\Roaming\Macromedia\Flash Player\www.macromedia.com\bin\
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.