Home All Groups Group Topic Archive Search About

How to filter by File Type ?

Author
27 Jun 2006 2:54 PM
Rob
I want to copy files from one folder to another... but only if it is a
".txt" file...

Below should work, but how would you filter on ".txt" files only ?

Thanks !

Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
        ' Process the list of files found in the directory.
        Dim fileName As String
        For Each fileName In fileEntries
           ' Do the File.copy here

        Next fileName

Author
27 Jun 2006 3:20 PM
Kerry Moorman
Rob,

Directory.GetFiles has an overloaded version that lets you specify the
search criteria, such as "*.txt".

Kerry Moorman


Show quoteHide quote
"Rob" wrote:

> I want to copy files from one folder to another... but only if it is a
> ".txt" file...
>
> Below should work, but how would you filter on ".txt" files only ?
>
> Thanks !
>
> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>         ' Process the list of files found in the directory.
>         Dim fileName As String
>         For Each fileName In fileEntries
>            ' Do the File.copy here
>
>         Next fileName
>
>
>
Author
27 Jun 2006 4:08 PM
Rob
The code below is returning the complete path of the file as well as the
filename.... Any way to return only the filename without the path ?

Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
         ' Process the list of files found in the directory.
         Dim fileName As String
         For Each fileName In fileEntries
            ' Do the File.copy here

         Next fileName



Show quoteHide quote
"Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
news:66AC3913-0536-4D8D-8B6E-DDF7D7F0EEC7@microsoft.com...
> Rob,
>
> Directory.GetFiles has an overloaded version that lets you specify the
> search criteria, such as "*.txt".
>
> Kerry Moorman
>
>
> "Rob" wrote:
>
>> I want to copy files from one folder to another... but only if it is a
>> ".txt" file...
>>
>> Below should work, but how would you filter on ".txt" files only ?
>>
>> Thanks !
>>
>> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>>         ' Process the list of files found in the directory.
>>         Dim fileName As String
>>         For Each fileName In fileEntries
>>            ' Do the File.copy here
>>
>>         Next fileName
>>
>>
>>
Author
27 Jun 2006 4:19 PM
Kerry Moorman
Rob,

You could use the Path class's GetFileName method.

Kerry Moorman


Show quoteHide quote
"Rob" wrote:

> The code below is returning the complete path of the file as well as the
> filename.... Any way to return only the filename without the path ?
>
> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>          ' Process the list of files found in the directory.
>          Dim fileName As String
>          For Each fileName In fileEntries
>             ' Do the File.copy here
>
>          Next fileName
>
>
>
> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
> news:66AC3913-0536-4D8D-8B6E-DDF7D7F0EEC7@microsoft.com...
> > Rob,
> >
> > Directory.GetFiles has an overloaded version that lets you specify the
> > search criteria, such as "*.txt".
> >
> > Kerry Moorman
> >
> >
> > "Rob" wrote:
> >
> >> I want to copy files from one folder to another... but only if it is a
> >> ".txt" file...
> >>
> >> Below should work, but how would you filter on ".txt" files only ?
> >>
> >> Thanks !
> >>
> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
> >>         ' Process the list of files found in the directory.
> >>         Dim fileName As String
> >>         For Each fileName In fileEntries
> >>            ' Do the File.copy here
> >>
> >>         Next fileName
> >>
> >>
> >>
>
>
>
Author
27 Jun 2006 5:19 PM
Rob
Kerry,

Almost there... just one more piece... the line with leading ***** below
filtering on ".txt" in a loop ?

Thanks,
Rob

Dim fileEntries As String() = Directory.GetFiles(strEODFolderSrc)

Dim fSource As String

Dim fDest As String

' Process the list of files found in the directory.

Dim fileName As String

For Each fileName In fileEntries

   fSource = strEODFolderSrc & Path.GetFileName(fileName)

    fDest = strEODFolderDest & Path.GetFileName(fileName)

        '   Only show text files ????

'********   If the file extention of Path.GetFileName(fileName) Then

                    MsgBox("Source:" & fSource)

                      MsgBox("Dest:" & fDest)

                End If

Next fileName



Show quoteHide quote
"Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
news:D6001ED7-7576-4CBE-A101-9489802856B7@microsoft.com...
> Rob,
>
> You could use the Path class's GetFileName method.
>
> Kerry Moorman
>
>
> "Rob" wrote:
>
>> The code below is returning the complete path of the file as well as the
>> filename.... Any way to return only the filename without the path ?
>>
>> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>>          ' Process the list of files found in the directory.
>>          Dim fileName As String
>>          For Each fileName In fileEntries
>>             ' Do the File.copy here
>>
>>          Next fileName
>>
>>
>>
>> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
>> news:66AC3913-0536-4D8D-8B6E-DDF7D7F0EEC7@microsoft.com...
>> > Rob,
>> >
>> > Directory.GetFiles has an overloaded version that lets you specify the
>> > search criteria, such as "*.txt".
>> >
>> > Kerry Moorman
>> >
>> >
>> > "Rob" wrote:
>> >
>> >> I want to copy files from one folder to another... but only if it is a
>> >> ".txt" file...
>> >>
>> >> Below should work, but how would you filter on ".txt" files only ?
>> >>
>> >> Thanks !
>> >>
>> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>> >>         ' Process the list of files found in the directory.
>> >>         Dim fileName As String
>> >>         For Each fileName In fileEntries
>> >>            ' Do the File.copy here
>> >>
>> >>         Next fileName
>> >>
>> >>
>> >>
>>
>>
>>
Author
27 Jun 2006 5:36 PM
Kerry Moorman
Rob,

You could use Path.GetExtension (fileName) to get the file's extension.

But as I mentioned in my first reply, you could also use:

  Dim fileEntries As String() = Directory.GetFiles(strEODFolderSrc, "*.txt")

to just retrieve and process files with a ".txt" extension.

Kerry Moorman


Show quoteHide quote
"Rob" wrote:

> Kerry,
>
> Almost there... just one more piece... the line with leading ***** below
> filtering on ".txt" in a loop ?
>
> Thanks,
> Rob
>
> Dim fileEntries As String() = Directory.GetFiles(strEODFolderSrc)
>
> Dim fSource As String
>
> Dim fDest As String
>
> ' Process the list of files found in the directory.
>
> Dim fileName As String
>
> For Each fileName In fileEntries
>
>    fSource = strEODFolderSrc & Path.GetFileName(fileName)
>
>     fDest = strEODFolderDest & Path.GetFileName(fileName)
>
>         '   Only show text files ????
>
> '********   If the file extention of Path.GetFileName(fileName) Then
>
>                     MsgBox("Source:" & fSource)
>
>                       MsgBox("Dest:" & fDest)
>
>                 End If
>
> Next fileName
>
>
>
> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
> news:D6001ED7-7576-4CBE-A101-9489802856B7@microsoft.com...
> > Rob,
> >
> > You could use the Path class's GetFileName method.
> >
> > Kerry Moorman
> >
> >
> > "Rob" wrote:
> >
> >> The code below is returning the complete path of the file as well as the
> >> filename.... Any way to return only the filename without the path ?
> >>
> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
> >>          ' Process the list of files found in the directory.
> >>          Dim fileName As String
> >>          For Each fileName In fileEntries
> >>             ' Do the File.copy here
> >>
> >>          Next fileName
> >>
> >>
> >>
> >> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
> >> news:66AC3913-0536-4D8D-8B6E-DDF7D7F0EEC7@microsoft.com...
> >> > Rob,
> >> >
> >> > Directory.GetFiles has an overloaded version that lets you specify the
> >> > search criteria, such as "*.txt".
> >> >
> >> > Kerry Moorman
> >> >
> >> >
> >> > "Rob" wrote:
> >> >
> >> >> I want to copy files from one folder to another... but only if it is a
> >> >> ".txt" file...
> >> >>
> >> >> Below should work, but how would you filter on ".txt" files only ?
> >> >>
> >> >> Thanks !
> >> >>
> >> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
> >> >>         ' Process the list of files found in the directory.
> >> >>         Dim fileName As String
> >> >>         For Each fileName In fileEntries
> >> >>            ' Do the File.copy here
> >> >>
> >> >>         Next fileName
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>
Author
27 Jun 2006 6:31 PM
Rob
Thanks very much Kerry !


Show quoteHide quote
"Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
news:5CF9E8B9-F2EB-4426-9C09-800ECB005B79@microsoft.com...
> Rob,
>
> You could use Path.GetExtension (fileName) to get the file's extension.
>
> But as I mentioned in my first reply, you could also use:
>
>  Dim fileEntries As String() = Directory.GetFiles(strEODFolderSrc,
> "*.txt")
>
> to just retrieve and process files with a ".txt" extension.
>
> Kerry Moorman
>
>
> "Rob" wrote:
>
>> Kerry,
>>
>> Almost there... just one more piece... the line with leading ***** below
>> filtering on ".txt" in a loop ?
>>
>> Thanks,
>> Rob
>>
>> Dim fileEntries As String() = Directory.GetFiles(strEODFolderSrc)
>>
>> Dim fSource As String
>>
>> Dim fDest As String
>>
>> ' Process the list of files found in the directory.
>>
>> Dim fileName As String
>>
>> For Each fileName In fileEntries
>>
>>    fSource = strEODFolderSrc & Path.GetFileName(fileName)
>>
>>     fDest = strEODFolderDest & Path.GetFileName(fileName)
>>
>>         '   Only show text files ????
>>
>> '********   If the file extention of Path.GetFileName(fileName) Then
>>
>>                     MsgBox("Source:" & fSource)
>>
>>                       MsgBox("Dest:" & fDest)
>>
>>                 End If
>>
>> Next fileName
>>
>>
>>
>> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in message
>> news:D6001ED7-7576-4CBE-A101-9489802856B7@microsoft.com...
>> > Rob,
>> >
>> > You could use the Path class's GetFileName method.
>> >
>> > Kerry Moorman
>> >
>> >
>> > "Rob" wrote:
>> >
>> >> The code below is returning the complete path of the file as well as
>> >> the
>> >> filename.... Any way to return only the filename without the path ?
>> >>
>> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>> >>          ' Process the list of files found in the directory.
>> >>          Dim fileName As String
>> >>          For Each fileName In fileEntries
>> >>             ' Do the File.copy here
>> >>
>> >>          Next fileName
>> >>
>> >>
>> >>
>> >> "Kerry Moorman" <KerryMoor***@discussions.microsoft.com> wrote in
>> >> message
>> >> news:66AC3913-0536-4D8D-8B6E-DDF7D7F0EEC7@microsoft.com...
>> >> > Rob,
>> >> >
>> >> > Directory.GetFiles has an overloaded version that lets you specify
>> >> > the
>> >> > search criteria, such as "*.txt".
>> >> >
>> >> > Kerry Moorman
>> >> >
>> >> >
>> >> > "Rob" wrote:
>> >> >
>> >> >> I want to copy files from one folder to another... but only if it
>> >> >> is a
>> >> >> ".txt" file...
>> >> >>
>> >> >> Below should work, but how would you filter on ".txt" files only ?
>> >> >>
>> >> >> Thanks !
>> >> >>
>> >> >> Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
>> >> >>         ' Process the list of files found in the directory.
>> >> >>         Dim fileName As String
>> >> >>         For Each fileName In fileEntries
>> >> >>            ' Do the File.copy here
>> >> >>
>> >> >>         Next fileName
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>
>>
>>