Click to See Complete Forum and Search --> : read directory names
jrthor2
04-07-2004, 12:36 PM
I have a directory structure that looks like this:
/events/images/vbs_2002
/events/images/vbs_2003
/events/images/vbs_2004
I have a page with alist box that I wan to populate with only the years of images I have, so the list box would have the following options:
2003
2003
2004
How can I accomplish this?
thanks
buntine
04-07-2004, 01:14 PM
Try something like this using the FileSystemObject.
const DIR = "\events\images\"
dim i, intLowed, intUpper
dim objFSO
objFSO = Server.CreateObject("Scripting.FileSystemObject")
'|Find the lower bound.
for i = 1900 to 3000
if objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i)) then
intLower = i
intUpper = i '|temp.
exit for
end if
next
'|Find the upper bound.
while objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i))
intUpper = intUpper + 1
i = i + 1
wend
'|Write the select box.
with response
.Write ("<select name=""ctrlSelect"">")
for i = intLower to intUpper
.Write (vbCrLf & vbTab & "<option>" & i & "</option>")
next
.Write (vbCrLf & "</select>")
end with
Havent tested it.. But it should give you an idea.
Regards,
Andrew Buntine.
jrthor2
04-07-2004, 01:22 PM
I get
Microsoft VBScript runtime error '800a01b6'
Object doesn't support this property or method
on this line:
objFSO = Server.CreateObject("Scripting.FileSystemObject")
UPDATE:
fixed the above error using:
set objFSO = CreateObject("Scripting.FileSystemObject")
Why am I getting the year 2005, I don't have a directory for that year yet?
buntine
04-07-2004, 01:34 PM
Hry, it didnt work cause i forgot the set keyword. You should still use Server.CreateObject rather than just CreateObject, though.
What is the script outputing? 2002, 2003, 2004, 2005 ??
What is it supposed to output? 2002, 2003, 2004 ??
Regards.
jrthor2
04-07-2004, 01:37 PM
it is outputting 2003, 2003, 2004, 2005.
It should be 2002, 2003, 2004.
also, anyway to have what the user selected and the selected list item after they choos a year?
buntine
04-07-2004, 01:41 PM
Here is the code. This should work good ;)
const DIR = "\events\images\"
dim i, intLowed, intUpper
dim objFSO
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'|Find the lower bound.
for i = 1900 to 3000
if objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i)) then
intLower = i
intUpper = i '|temp.
exit for
end if
next
'|Find the upper bound.
while objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i))
intUpper = intUpper + 1
i = i + 1
wend
'|Write the select box.
with response
.Write ("<select name=""ctrlSelect"">")
for i = intLower to intUpper - 1
.Write (vbCrLf & vbTab & "<option>" & i & "</option>")
next
.Write (vbCrLf & "</select>")
end with
Regarding having a certain member selected, how would you like to acheive this? Has the user already selected one? Can you just give a little more detail there, cause im not completely sure what you mean.
Regards,
Andrew Buntine.
jrthor2
04-07-2004, 01:47 PM
Actually, what I would like to see is
When you come in to the page, the first option is blank.
when a user selects an option, it loads the page, and the list box has the year they selected as the "selected" item in the list box.
The reason for the blank option is because when you come into the page and 2002 is the first option, you can't select that option.
buntine
04-07-2004, 02:00 PM
Here.
const DIR = "\events\images\"
dim i, intLowed, intUpper
dim objFSO
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'|Find the lower bound.
for i = 1900 to 3000
if objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i)) then
intLower = i
intUpper = i '|temp.
exit for
end if
next
'|Find the upper bound.
while objFSO.folderExists(Server.MapPath(DIR & "vbs_" & i))
intUpper = intUpper + 1
i = i + 1
wend
'|Write the HTML form.
with response
.Write ("<form method=""get"" action=""" & Split(Request.ServerVariables("SCRIPT_NAME"), "?")(0) & """ name=""frm"">" & vbCrLf)
.Write ("<select name=""ctrlSelect"">")
.Write ("<option")
if isEmpty(Request.QueryString("ctrlSelect")) then
.Write (" selected=""selected""")
end if
.Write ("></option>")
for i = intLower to intUpper - 1
.Write (vbCrLf & vbTab & "<option")
if CInt(Request.QueryString("ctrlSelect")) = i then
.Write (" selected=""selected""")
end if
.Write (">" & i & "</option>")
next
.Write (vbCrLf & "</select>")
.Write (vbCrLf & "</form>")
end with
Try that out.
Regards,
Andrew Buntine.
jrthor2
04-07-2004, 02:06 PM
I've changed my code around a bit, but put your changes in where I think they should go, but it still doesn't have the users selected item selected after they make a selection.
<form name="vbsyear" method="post" action="">
<select name="year" onChange="submit()">
<option value="selected">Select year --></option>
<%
const VBS_DIR = "\events\images\"
dim i, intLowed, intUpper
dim objFSO
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'|Find the lower bound.
for i = 1900 to 3000
if objFSO.folderExists(Server.MapPath(VBS_DIR & "VBS_" & i)) then
intLower = i
intUpper = i '|temp.
exit for
end if
next
'|Find the upper bound.
while objFSO.folderExists(Server.MapPath(VBS_DIR & "VBS_" & i))
intUpper = intUpper + 1
i = i + 1
wend
'|Write the select box.
with response
for i = intLower to intUpper - 1
.Write (vbCrLf & vbTab & "<option")
if CInt(Request.form("year") = i) then
.Write ("selected=""selected""")
end if
.Write (">" & i & "</option>")
next
end with
%>
</select>
</form>
buntine
04-07-2004, 02:10 PM
thats cause you changed the code...
if CInt(Request.form("year") = i) then
.Write ("selected=""selected""")
end if
should be
if CInt(Request.form("year")) = i then
.Write (" selected=""selected""")
end if
Regards.
jrthor2
04-07-2004, 02:11 PM
I fixed it, thanks for your help!!
buntine
04-07-2004, 02:12 PM
No worries. Is it all working fine now?
jrthor2
04-07-2004, 02:15 PM
yeppers :-)
jrthor2
04-07-2004, 02:25 PM
buntine,
Just tried something to test your code. I created a directory as VBS_2010 and it did not show up in my listbox. I think the code you wrote, the years must be in order, but what if a year goes by and I don't have any images for that year? I thought I could just read in the folder names and then just read from the right for a length of 4 or something like that?
buntine
04-07-2004, 02:32 PM
hmm, ok. I understand what you mean.
Look for this statement: exit for.
Remove it and trt again. If that doesnt work then we will have to put some more effort into it.
Regards,
Andrew Buntine.
jrthor2
04-07-2004, 02:35 PM
I removed that statement and now the list box doesn't display any years.
<form name="vbsyear" method="post" action="">
<select name="year" onChange="submit()">
<option value="0">Select Year --></option>
<%
const VBS_DIR = "\events\images\"
dim i, intLowed, intUpper
dim objFSO
set objFSO = Server.CreateObject("Scripting.FileSystemObject")
'|Find the lower bound.
for i = 1900 to 3000
if objFSO.folderExists(Server.MapPath(VBS_DIR & "VBS_" & i)) then
intLower = i
intUpper = i '|temp.
'exit for
end if
next
'|Find the upper bound.
while objFSO.folderExists(Server.MapPath(VBS_DIR & "VBS_" & i))
intUpper = intUpper + 1
i = i + 1
wend
'|Write the select box.
with response
for i = intLower to intUpper - 1
.Write (vbCrLf & vbTab & "<option")
if CInt(Request.form("year")) = i then
.Write (" selected")
end if
.Write (">" & i & "</option>")
next
end with
%>
</select>
</form>
buntine
04-07-2004, 02:39 PM
Ok, bad idea. Now that i look at it, that wouldnt work anyway :D
Is there only 'vbs_xxxx' directorys in the 'images' directory? If so, we could just use FSO to write the last 4 characters (the year) from each directory in the 'images' folder.
You understand?
Regards.
jrthor2
04-07-2004, 02:42 PM
unfortunately, no. there a bunch of directories in events\images.
buntine
04-07-2004, 02:45 PM
Damn.. Its still quite possible. But i gotta go to bed now (Got a Java exam tomorrow).
I will have a think over the next few hours and write something tomorrow morning.
Regards,
Andrew Buntine.
buntine
04-07-2004, 02:48 PM
One last thing.
Will there be directories for the future years? So if the year is 2005, will there be a directory for 2008? Or will that only be created in 2008?
Regards.
jrthor2
04-07-2004, 03:51 PM
How about I make a directory in /events/images called vbs_pics and in there just have 2002, 2003, 2004, etc. If that would be easier, I can do that if you can help me with the code. The base directory would be /events/images/vbs_pics with subdirectories of years.
There could be future years, so this is 2004, I could create a 2010 directory for instance.
thanks again.
jrthor2
04-08-2004, 11:49 AM
I have all my images in /events/images/vbs_pics/year. Could someone help me read the /events/images/vbs_pics directories and print out the years that we have?
Thanks
buntine
04-08-2004, 11:52 AM
ok, so you will have a dir listing like this:
/events/images/vbs_pics/2002
/events/images/vbs_pics/2003
/events/images/vbs_pics/2004
etc, etc. ??
Regards.
jrthor2
04-08-2004, 11:54 AM
yes, that is how my directory structure looks...
buntine
04-08-2004, 01:22 PM
Ok, i hacked away on my development machine for a while and came up with this:
<%
'| Project: Read Directory Listing.
'| Created: April 9th, 2004.
'| Last Mod: April 9th, 2004.
Dim strDirPath '|String. The path which contains the sub directories.
Dim objFSO, objDir, objSubDirs '|Objects. FSO-related.
Dim arrDirs() '|Array. Holds the name of each directory.
Dim i '|Integer. Looping incrementation variable.
i = 0
With Server
strDirPath = .MapPath("\asp_scripts\vbs_pics") '|You will need to edit this to suit your server.
Set objFSO = .CreateObject("Scripting.FileSystemObject")
End With
Set objDir = objFSO.getFolder(strDirPath)
Set objSubDirs = objDir.SubFolders
'|Construct the array of directory names.
For Each objFolder in objSubDirs
ReDim preserve arrDirs(i)
arrDirs(i) = CStr(objFolder.name)
i = i + 1
Next
'|Write the HTML form.
With Response
.Write ("<form action=""" & _
Split(Request.ServerVariables("SCRIPT_NAME"), "?")(0) & _
""" method=""get"" name=""objForm"">" & vbCrLf)
.Write ("<select name=""ctrlSelect"" onchange=""submit()"">" & vbCrLf)
.Write (vbTab & "<option>Choose a year</option>" & vbCrLf)
For i = 0 to UBound(arrDirs)
.Write (vbTab & "<option")
if CStr(Request.QueryString("ctrlSelect")) = arrDirs(i) then
.Write (" selected=""selected""")
end if
.Write (">" & arrDirs(i) & "</option>" & vbCrLf)
Next
.Write ("</select>" & vbCrLf)
.Write ("</form>" & vbCrLf)
End With
'|Close objects to free server resources.
Set objFSO = Nothing
Set objDir = Nothing
Set objSubDirs = Nothing
%>
Change to suit, and it should work fine ;) It worked on my machine.
Regards,
Andrew Buntine.
jrthor2
04-08-2004, 01:35 PM
thanks buddy, it works like a champ!!!