How to filter by File Type ?

R

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
 
G

Guest

Rob,

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

Kerry Moorman
 
R

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
 
R

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
 
G

Guest

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
 
R

Rob

Thanks very much Kerry !


Kerry Moorman said:
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
 

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

Top