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.

Code: [Select]Set foldercounter=0
For /F %%a in ('Fsutil Fsinfo Drives') do ( Set d=%%a
call If "%%d:~1%%"==":\" call If exist %%d%%TEST (call set folder_%%foldercounter%%=%%d%%TEST & set /a foldercounter+=1)
)
if %foldercounter%==0 goto end
set foldercounter2=0
:loop
set foldermalformedname=folder_%foldercounter2%
call echo/%%%foldermalformedname%%%
If not %foldercounter%==%foldercounter2% set/a foldercounter2+=1 & goto loop
:end
Let's say I have  have three drives: C:\, D:\, F:\ and I:\.  Only I:\ has a TEST folder in its root directory. I would expect the output:

Code: [Select]I:\TEST
Currently, I'm unable to find a way to chain my if statements without causing errors (the above code throws errors, despite many MODIFICATION attempts). Also, how would I then be able to include drives that had, for example, a folder entitled MISC in their root directory? Let's say, continuing from the above example, that both I:\ and F:\ had a MISC directory in them. How could I produce the output below?

Code: [Select]F:\MISC
I:\TEST
I:\MISC
I'm now aware that I should have chosen Powershell or similar for the job and I've already been reprimanded for it:

Quote from: Salmon Trout on January 05, 2012, 01:12:16 PM

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


Discussion

No Comment Found