For each ..... next Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

My application browses the contents of a network directory and displays any
powerpoint file found in the directory. On completion it returns to the start
and begins again.

The following is a snippet of the code that is used to get the name of the
file before it is displayed.

Dim fs, f, fc, f1
Dim PresentationName as String
fs = CreateObject("Scripting.FileSystemObject")
f = fs.GetFolder(***********)
fc = f.files

f1 = Nothing

For Each f1 in fc
PresentationName = f1.Name
'lots of other code follows
Next

For several reasons, which I won't go into here, I also need to know the
name of the next file in the directory as well as the name of the current one
at the same time ..... so my code needs to be changed as follows

For Each f1 in fc
PresentationName = f1.Name
' some code to get the name of the next file (i.e. the
next f1)
'lots of other code follows
Next

Can anybody help with the line where I need "some code to get the name of
the next file"

Would appreciate any pointers

Regards

Michael Bond
 
Hi,

I hate late binding it causes a lot of hard to find errors. Always
declare a type when you dim a variable. Anyway I would use the
directoryinfo class to list the files rather than the file system object.
This sample will list all the powerpoint files in my documents.

Dim fi As System.IO.FileInfo
Dim di As New
System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
For Each fi In di.GetFiles("*.ppt")
Trace.WriteLine(fi.Name)
Next


Ken
 
Ken

thanks for those pointers on my existing code. Apprciate that advice to
clean up my code and I'll follow through on that....

In the meantime I'm still short of a suggestion on how to find the name of
the next file .... any ideas

Regards

Michael
 
Hi,

Dim files() As System.IO.FileInfo
Dim di As New
System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments))
files = di.GetFiles("*.ppt")
Dim intLast As Integer = files.GetUpperBound(0)
For x As Integer = 0 To intLast
Dim strOut As String
If x < intLast Then
strOut = files(x + 1).Name
Else
strOut = "Last File"
End If
Trace.WriteLine(String.Format("This file {0} Next File {1}",
files(x).Name, strOut))
Next


Ken
 
Ken

thanks

Changed my code per your suggestion. Incorporated your guidance on finding
the "next" file name and was also able to adapt to return the first file name
in the directory when it reached the last filename, which was need becasue of
my loop back to the begining. Much tidier now and does what I need it to do.
Thanks for the help

Regards

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top