| 1. |
Solve : Chained ifs within a for statement.? |
|
Answer» I am attempting to store a list of drives which possess a TEST directory in their root folder within a pseudo-array. For the purpose of this example, I would then want them read back. Question: If you feel the need to laboriously create pseudo-arrays, and you find batch syntax "cumbersome", why not use a language with proper arrays and cool looking code, like VBScript or, better, Powershell? This is the last bit of scripting needed now, though, and I'd really rather not learn a new language and then rewrite over 200 lines of script (some of it pretty convoluted) just to avoid ugly code. Quote from: Sirim on January 07, 2012, 12:05:30 PM Let's say I have have three drives: C:\, D:\, F:\ and I:\. I can count, honest. I added a fourth drive in when I wanted a second example, but forgot to replace the 'three' with 'four'. echo off for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( if exist "%%A:\" ( if exist %%A:\Test echo %%A:\Test EXISTS if exist %%A:\Info echo %%A:\Info exists ) ) The pseudo-array CREATION was required; the echoing it out was just for the example. Working code below: Code: [Select]echo off Set foldercounter=0 for %%A in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do ( if exist "%%A:\" ( if exist %%A:\Misc call Set folder_%%foldercounter%%=%%A:\Misc & Set/a foldercounter+=1 if exist %%A:\Test call Set folder_%%foldercounter%%=%%A:\Test & Set/a foldercounter+=1 ) ) if %foldercounter%==0 goto end set foldercounter2=0 :loop set foldermalformedname=folder_%foldercounter2% call echo/%%%foldermalformedname%%% set/a foldercounter2+=1 If not %foldercounter%==%foldercounter2% goto loop :end |
|