Page 1 of 1

Help with ECHO variable after FOR-IN-DO(SET /A [var]%%f)

Posted: 06 Dec 2011 14:02
by Patte
Hi!

I have som problem with my code...

Im trying to generate variables using a FOR-IN-DO.

Code: Select all

@ECHO OFF

REM -- This .bat should count all subfolders of the folders specified in the
REM -- in the "TARGET_FOLDER_LIST".
REM -- The sub-routine "countAllFolders" shall generate a unique counter-variable
REM -- for each Folder using a "For In Do".
REM --
REM -- After the counting, the generated counter-variable should be ECHO:ed

SET WORK_PATH=%CD%

SET TARGET_FOLDER_LIST=(Folder1 Folder2 Folder3)
SET SOURCE_PATH=%CD%


FOR %%f IN %TARGET_FOLDER_LIST% DO (
   SET /A COUNTER_%%f=0
   ECHO %SOURCE_PATH%\%%f
   call :countAllFolders "%SOURCE_PATH%\%%f" "%%f"
REM ------------------ Here is where I get problem --------------------
   ECHO COUNTER_%%f
)

GOTO :eof

:countAllFolders
FOR /R %1 %%i IN (.) DO SET /A COUNTER_%2+=1
GOTO :eof


It all works great untill I try to ECHO on the generated variable inside the first FOR-IN-DO

This:

Code: Select all

   ECHO COUNTER_%%f

Will only get me the output "COUNTER_Folder[X]"

And this:

Code: Select all

   ECHO %COUNTER_%%f%

Will only get me the output "ECHO is off"

If I add this to my code:

Code: Select all

ECHO %COUNTER_Folder1%
ECHO %COUNTER_Folder2%
ECHO %COUNTER_Folder3%

The output will show a correct count of the subfolders in each Folder.

But my problem is that I need to get the output without specifying the variable-names in advance.

Does anyone have any idea how I should modify my code in order to for it to work properly?

Re: Help with ECHO variable after FOR-IN-DO(SET /A [var]%%f)

Posted: 06 Dec 2011 14:36
by dbenham
use SETLOCAL EnableDelayedExpansion near top
and use ECHO !COUNTER_%%f! edited

Your solution may experience problems if you run into special characters in path, but this should solve your immediate problem.


Dave Benham

Re: Help with ECHO variable after FOR-IN-DO(SET /A [var]%%f)

Posted: 07 Dec 2011 00:32
by orange_batch
Patte, like dbenham said, you can change @ECHO OFF at the top to @ECHO OFF&SETLOCAL EnableDelayedExpansion and ECHO COUNTER_%%f to ECHO !COUNTER_%%f!. Or, you can change ECHO COUNTER_%%f to CALL ECHO %%COUNTER_%%f%% (slower but no need for delayed expansion).

As for your code, I don't recommend using a subroutine as call is slow. There is a more efficient way of doing this. (However, as you wrote, for /r is faster than dir /ad /b /s, another means for the same result.)

Hmm I see why you're making a call to FOR /R, it won't nest! I guess because command prompt processes FOR /R with it's target directory before expanding... That means, we have to use PUSHD and POPD to temporarily change the current directory, and return.

Using your style of syntax, I would write it as:

Code: Select all

@ECHO OFF&SETLOCAL EnableDelayedExpansion

:: This .bat should count all subfolders of the folders specified in the
:: in the "TARGET_FOLDER_LIST".
:: It will generate a unique counter-variable for each Folder using a "For In Do".

:: After the counting, the generated counter-variable should be ECHO:ed.

SET "FOLDER_LIST=(Folder1 Folder2 Folder3)"

FOR %%f IN %FOLDER_LIST% DO (
   SET COUNTER_%%f=
   ECHO %CD%\%%f
   PUSHD %%f
   FOR /R %%i IN (.) DO SET /A COUNTER_%%f+=1
   POPD
   ECHO !COUNTER_%%f!
)

GOTO :eof

Re: Help with ECHO variable after FOR-IN-DO(SET /A [var]%%f)

Posted: 07 Dec 2011 06:48
by dbenham
Of course, how silly of me :oops:

Re: Help with ECHO variable after FOR-IN-DO(SET /A [var]%%f)

Posted: 12 Dec 2011 11:21
by Patte
Thanks orange_batch and dbenham!

orange_batch, your modification on my code works just great :D