|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to filter by File Type ?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 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 > > > 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 >> >> >> 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 > >> > >> > >> > > > 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 >> >> >> >> >> >> >> >> >> 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 > >> >> > >> >> > >> >> > >> > >> > >> > > > 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 >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> >> |
|||||||||||||||||||||||