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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Patte
Posts: 2
Joined: 06 Dec 2011 12:56

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

#1 Post by Patte » 06 Dec 2011 14:02

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?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#2 Post by dbenham » 06 Dec 2011 14:36

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
Last edited by dbenham on 07 Dec 2011 06:49, edited 1 time in total.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

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

#3 Post by orange_batch » 07 Dec 2011 00:32

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
Last edited by orange_batch on 07 Dec 2011 07:25, edited 1 time in total.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

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

#4 Post by dbenham » 07 Dec 2011 06:48

Of course, how silly of me :oops:

Patte
Posts: 2
Joined: 06 Dec 2011 12:56

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

#5 Post by Patte » 12 Dec 2011 11:21

Thanks orange_batch and dbenham!

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

Post Reply