Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading first file in a folder 1

Status
Not open for further replies.

Gatorajc

MIS
Mar 1, 2002
423
US
I must have gone drain bead.

I want the name of the first file in a folder. I know how to go through a folder.

For Each objFile In objFolder.Files

and get the name

Set objfile = objFSO.GetFile(log.txt)

but what if I do not know the name of the first file. Still learning the filesytemobject thing.


AJ
[americanflag]


 
You mean like this? if you take out for each... and next that would give you the first.
<%
dim fs,fo,x
set fs=Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set fo=fs.GetFolder(&quot;c:\folder\&quot;)
for each x in fo.files
'Print the name of all files in the foldername folder
Response.write(x.Name & &quot;<br />&quot;)
next
set fo=nothing
set fs=nothing
%>
 
I can do that and it works but I want just the first file name in the folder none of the others.

Say these are the files in x folder

log.txt
90909.tif
593993.tif

I want to be able to just get log.txt. I now I could cycle through all of them to get it but I can not figure out the logic and/or code to just get the first file.



AJ
[americanflag]


 
Define first. First sorted by name, date? In your example the first file is 593993.tif if you use the above code.

 
By name. It is the windows default. Yea I gave a bad example with those files. You are right 593993.tif would be the one I would want to get.


AJ
[americanflag]


 
going off stevedi's code, you can use an exit for

<%
dim fs,fo,x
set fs=Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
set fo=fs.GetFolder(&quot;c:\folder\&quot;)
loopnum=1
for each x in fo.files
'Print the name of all files in the foldername folder
Response.write(x.Name & &quot;<br />&quot;)
if loopnum=1 then exit for
next
set fo=nothing
set fs=nothing
%>

that'll go through one loop. I'm sure there is an easier way, but that does the trick off the top of my head
 
Thats pretty easy, I had thought about something like that, but I was hoping for something more efficient but that will do thanks.


AJ
[americanflag]


 
I was thinking you could get the first item in the files collection by index, but apparently VBScript doesn't treat that as a true collection or I am to tired after work to see what I am doing wrong. If no one else sees what is wrong with the following code the the escaped loop method would be your best bet:
Code:
'non-working code
<%
Dim fso

Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)

Dim fol
set fol = fso.GetFolder(Server.MapPath(&quot;.&quot;))

Response.Write &quot;The first File is: &quot; & fol.Files.item(0) 
Set fol = Nothing
Set fso = Nothing
& &quot;!&quot;
%>

-Tarwn

[sub]01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101 [/sub]
[sup]29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19[/sup]
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top