0

The objective is to display the directory name in MYFOLDER. MY.exe exists in the folder, but curiously, without the wildcard in ...\desktemp*, the "@echo Showing subfolders" is never displayed, but "@echo G is working" is. However MY.exe is never found when moved to one of the subfolders.

OTOH the current code never finds MY.exe and never displays "@echo G is working" but properly lists each subfolder: "@echo Showing subfolders".

The other problem is the pauses at the end of the block are never reached.

Substituting the inner For with

cd \Users\%USERNAME%\Desktop
for /D /r %%G in ("desktemp*") do (

gets essentially the same result. My.exe isn't found if moved to one of the subfolders of desktemp.

Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES


REM BLOCK

for %%B in (C) do (
if exist %%B: (

PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\ 

for /f "tokens=*" %%G in ('dir /b /s /a:d "%%B:\Users\%USERNAME%\Desktop\desktemp*" ^| find "\"') do (
@echo Showing subfolders
@echo %%G
pause
if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
@echo %%G
@echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)

)
REM Exist Drive
)
REM Drives Loop


:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd  %MYFOLDER%
pause

The above is a chunk whittled from a larger code block: the ultimate aim will be to get the folder names"\Users\New\Desktop\desktemp" into a variable via prompt. Are the Escape Characters, Delimiters and Quotes in the nested blocks implemented properly?

Laurie Stearn
  • 959
  • 1
  • 13
  • 34
  • In a `Bat` file you have to double the `%` of the `FOR` Variables. `For %%B in (c) do (if exist %%B......` – SachaDee Dec 02 '14 at 00:50
  • Yes, I kept these as singles to get the error messages. I have logging setup, but doesn't capture the important stuff. Is there a good resource anywhere on batch error logging? – Laurie Stearn Dec 02 '14 at 03:39
  • There are so many errors, it's difficult to make headway. The fundamental problem is that `%x` will generate a syntax-error if you attempt to use it as a metavariable, as you have already ben advised. Regardless of your desire to get the error messages, until those errors are rectified, the batch simply will not run. There's no point to chasing escape characters, etc. until you do this - the basic `for` structure you are using won't work. – Magoo Dec 02 '14 at 06:32
  • Hey, thanks for the hint Magoo, got it running as standalone batch. Still same issues, but at least it gets to the end now, but the return on cd !MYFOLDER! is still !MYFOLDER! unfortunately. – Laurie Stearn Dec 02 '14 at 08:42
  • The use of !MYFOLDER! outside the loop was wrong. Displaying %MYFOLDER% and using it in CD works now. Corrected the script. – Laurie Stearn Dec 02 '14 at 11:56
  • Oooooh right (bug or feature?) , using the above find command, ...\desktemp* does not find MY.exe when it lives in a subfolder of desktemp. But ...\desktemp\\* DOES. – Laurie Stearn Dec 03 '14 at 06:41
  • Another (undocumented) observation on dir /s is when a bunch of nested subfolders under desktemp are created all beginning with a letter "x". Then using ...\desktemp\x* will search all those subfolders and nested subfolders beginning with x but ignore all those not beginning with x. And those not beginning with x containing subfolders beginning with x are also ignored. – Laurie Stearn Dec 03 '14 at 08:07
  • "And those not beginning with x containing subfolders beginning with x are also ignored." should be not ignored. – Laurie Stearn Dec 03 '14 at 08:16
  • Aha: "And those not beginning with x containing subfolders beginning with x are also ignored" applies to the "for /D /r %%G in ("x*")" command. – Laurie Stearn Dec 03 '14 at 08:31

1 Answers1

0

The answer escaped this poor little brain until it cottoned on to what the "DIR" and "For /D /R" were really up to. What was sought for was in the addition of a new "For /D" (no /R).

This first (extra) "For /D" determines the folder names to iterate from. (Specifically anything but the Windows directory where we run into problems with >260 filenames.)

This locates the MY.exe file somewhere in the Users folder (more precisely in any root folder beginning with U):

Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES


REM BLOCK

for %%B in (C) do (
if exist %%B: (

PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\ 
for /D %%Z in (U*) do (

cd \%%Z
for /D /r %%G in ("*") do (

if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
@echo %%G
@echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)
)

)
REM Exist Drive
)
REM Drives Loop


:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd  %MYFOLDER%
pause

Edit:

The source of the error spam in the comment below is this command:

Insert batch code to elevate UAC privileges [code][1] from TanisDL
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")

Laurie Stearn
  • 959
  • 1
  • 13
  • 34
  • This spits out "The directory name is too long" for such items as: "C:\Users\New\AppData\Local\Microsoft\VisualStudio\10.0\Extensions\Sam Harwell\ANTLR Language Support\1.2.1\~IC\ItemTemplates\CSharp\ANTLR\1033". Now this is only 143 chars long, but somehow it muffs the first part of the dir string after \Users\New with "Local Settings\Application Data\Application Data\Application Data\Application Data\Application Data\Application Data", neither of which "Local Settings" or "Application Data" exists! Only Appdata (Home Premium) – Laurie Stearn Dec 03 '14 at 13:35
  • Er, the above errors only occur when "Invoking UAC for Privilege Escalation" in the script: Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs" Another topic methinks. – Laurie Stearn Dec 03 '14 at 13:56
  • Forget the above two comments. The error with the UAC happens with______FOR /F "usebackq delims==" %%G IN (`dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "myString"`) DO (set "foundMyString=%%~pG")_______ – Laurie Stearn Dec 06 '14 at 11:27