1.

Solve : I'm stumpted.. why isn't this working??

Answer»

Here's my code below.  I've setup the batch file to read the directories in a folder, and then list them in ALPHABETICAL order with a number beside it so the user can then choose which folder they want to use.  The problem is that the counter (which numbers the folders) isn't progressing, and I can't figure out why!   


:ListFolders
SetLocal
Set Input=0
Set Count=1
Set Folder=%1
Echo Choose a Folder from Below:
For /F %%a in ('dir /b /o /a:d C:\MainFolder\%Folder%\*') Do (
   Echo %Count%  ==  %%a
   Set /a Count=Count+1
   )
Set /P Input=     SELECTION:


Is there something in the code I'm missing, or perhaps a global setting/variable that I don't have that I need?

Thanks. Quote from: Feidom on March 21, 2008, 03:02:43 PM

Is there something in the code I'm missing, or perhaps a global setting/variable that I don't have that I need?

See red items

:ListFolders
SetLocal enabledelayedexpansion
Set Input=0
Set Count=1
Set Folder=%1
Echo Choose a Folder from Below:
For /F %%a in ('dir /b /o /a:d C:\MainFolder\%Folder%\*') Do (
   Echo !Count!  ==  %%a
   Set /a Count=!Count!+1
   )
Set /P Input=     Selection:Just out of CURIOSITY, what does that line do exactly? Quote from: Feidom on March 21, 2008, 03:35:29 PM
Just out of curiosity, what does that line do exactly?

Normally, in a WINDOWS NT family batch file, ordinary variables (the ones with percent signs like this:  %variable%) are expanded just once, at run time. This includes variables inside loops. This means that a variable cannot be created or its value changed in a loop or a multiline IF STRUCTURE. To make that possible, Windows 2000 introduced a new feature - delayed expansion. To use this you need to do the following...

1. Enable delayed expansion by either
   
    (a) start a command shell using CMD /V:ON

    -- or --

    (b) include the line
         
         setlocal enabledelayedexpansion

         in a batch file before using delayed expansion

-- AND --

2. Use exclamation marks (!) instead of percent signs (%) around variables as in my suggested code above.


Type cmd /? at the prompt and/or Google for "delayed expansion" for more information, and very possibly a better explanation than I have given here.





Discussion

No Comment Found