Sunday, October 25, 2015

windows 7 - Batch Directory Name is too Long

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.

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...