[Closed] problem with 'for' loop
Hi guys!
I’m doing the Allan McKeys Technical Directors Transformation, I dont know if any of you guys does the course as well (great course for begginers to go pro in scripting very fast, by the way).
There is a code that gives me headaches. I’m not sure what is going on there, but this is the script:
/*
This script makes a rollout to list all the root directories on my local disc, eg. D:/NULL, D:/ProjectKickAss, ...
*/
-- setting the drive
YourDrive = "D:"
DrvDir = YourDrive + "/*"
readPdir = getdirectories DrvDir
-- for loop to get rid of unwanted directories
for i = 1 to readPDir.count do
(
[B]if (readPdir[i] == (YourDrive + "\\dev\\")) or (readPdir[i] == (YourDrive + "\\Renders\\")) then (deleteitem ReadPDir i)[/B]
)
-- cut the strings to display properly in the dropDownList (cutting out "D:\" and "\")
for i = 1 to readPdir.count do (ReadPdir[i] = (substring readPdir[i] 4 (readPdir[i].count - 4)))
-- the rollout to display the ReadPDir (directories on the YourDrive disc) array
rollout _ShotMakerUI "Shot Maker"
(
dropDownList _ProjectList "Project" items:readPdir
)
createdialog _ShotMakerUI
This script indeed works as long you have only two directories (to be removed from the readPdir array) on the disc. But usually you have al least two more, eg. $RECYCLE.BIN and System Volume Information. I guess it depends on your operating system, thats just a guess.
But even then if you want to remove more directories from the array, you need to change the script, I’ve changed this to sth like this. It does not look nice and professional, but it should work:
/*
This script makes a rollout to list all the root directories on my local disc, eg. D:/NULL, D:/ProjectKickAss, ...
*/
-- setting the drive
YourDrive = "D:"
DrvDir = YourDrive + "/*"
readPdir = getdirectories DrvDir
-- for loop to get rid of unwanted directories
for i = 1 to readPDir.count do
(
if (ReadPDir[i] == (YourDrive + "\\dev\\")) then (deleteitem ReadPDir i) -- 1.
if (ReadPDir[i] == (YourDrive + "\\Renders\\")) then (deleteitem ReadPDir i) -- 2.
if (ReadPDir[i] == (YourDrive + "\\PROGRAMY\\")) then (deleteitem ReadPDir i) -- 3.
if (ReadPDir[i] == (YourDrive + "\\$RECYCLE.BIN\\")) then (deleteitem ReadPDir i) -- 4.
if (ReadPDir[i] == (YourDrive + "\\System Volume Information\\")) then (deleteitem ReadPDir i) -- 5.
if (ReadPDir[i] == (YourDrive + "\\COMMERCIAL\\")) then (deleteitem ReadPDir i) -- 6.
)
-- cut the strings to display properly in the dropDownList (cutting out "D:\" and "\")
for i = 1 to readPdir.count do (ReadPdir[i] = (substring readPdir[i] 4 (readPdir[i].count - 4)))
-- the rollout to display the ReadPDir (directories on the YourDrive disc) array
rollout _ShotMakerUI "Shot Maker"
(
dropDownList _ProjectList "Project" items:readPdir
)
createdialog _ShotMakerUI
So I just make a test for each of the directory I want to remove from the readPdir array. Besically I got rid of every directory form the array (it should be empty), and then I manually created a NULL dir on my disc, just to keep one object in my array.
However this does not work. The readPdir.count is 3 not 1. And the order of the for loop affects the array. If you put row no1 and no2 in the end, the count is 2. And even one time I managed to shuffle the order, that the count really was 1.
I cannot find what is wrong with this for loop
Maybe I didnt make myself clear.
I’m not showing you a pro code but I’m asking for help, because the for loop is not working the way I want it to
This should get you started:
(
YourDrive = @"C:\"
fa = dotnetClass "System.IO.FileAttributes"
di = dotnetOBject "System.IO.DirectoryInfo" YourDrive
dirs = di.getDirectories()
arrDirs = for dir in dirs where not ((dir.Attributes.HasFlag fa.Hidden) or (dir.Attributes.HasFlag fa.ReadOnly) or (dir.Attributes.HasFlag fa.System)) collect dir.Name
rollout _ShotMakerUI "Shot Maker"
(
dropDownList _ProjectList "Project" items:arrDirs
)
createdialog _ShotMakerUI
)