Home All Groups Group Topic Archive Search About

For each ..... next Question

Author
5 Jan 2006 11:27 AM
mabond
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

Author
5 Jan 2006 11:57 AM
Ken Tucker [MVP]
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
---------------------------
Show quoteHide quote
"mabond" <mab***@discussions.microsoft.com> wrote in message
news:1D93A574-F7AD-407A-B2D0-F89BBAB11575@microsoft.com...
> 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
Author
5 Jan 2006 12:11 PM
mabond
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


Show quoteHide quote
"Ken Tucker [MVP]" wrote:

> 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
> ---------------------------
> "mabond" <mab***@discussions.microsoft.com> wrote in message
> news:1D93A574-F7AD-407A-B2D0-F89BBAB11575@microsoft.com...
> > 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
>
>
>
Author
5 Jan 2006 12:23 PM
Ken Tucker [MVP]
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
--------------
Show quoteHide quote
"mabond" <mab***@discussions.microsoft.com> wrote in message
news:665F09B5-83CF-400A-BD23-8AA0A708147D@microsoft.com...
> 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
>
>
> "Ken Tucker [MVP]" wrote:
>
>> 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
>> ---------------------------
>> "mabond" <mab***@discussions.microsoft.com> wrote in message
>> news:1D93A574-F7AD-407A-B2D0-F89BBAB11575@microsoft.com...
>> > 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
>>
>>
>>
Author
5 Jan 2006 1:00 PM
mabond
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


Show quoteHide quote
"Ken Tucker [MVP]" wrote:

> 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
> --------------
> "mabond" <mab***@discussions.microsoft.com> wrote in message
> news:665F09B5-83CF-400A-BD23-8AA0A708147D@microsoft.com...
> > 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
> >
> >
> > "Ken Tucker [MVP]" wrote:
> >
> >> 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
> >> ---------------------------
> >> "mabond" <mab***@discussions.microsoft.com> wrote in message
> >> news:1D93A574-F7AD-407A-B2D0-F89BBAB11575@microsoft.com...
> >> > 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
> >>
> >>
> >>
>
>
>