[RESOLVED] Copy Files using wildcards
Ok i want to copy all the files in a directory with an extension e.g .exe or maybe all them files *.* how do i do this?
I searched on google and found this on MSDN But it doesn't work when i type *. or *.* or *.exe into the textbox and there is files in that folder
VB.NET Code:
For Each foundFile As String In My.Computer.FileSystem.GetFiles(Application.StartupPath, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly, TextBox8.Text)
My.Computer.FileSystem.CopyFile(foundFile, IO.Path.Combine(FullPath, foundFile))
Next
Re: Copy Files using wildcards
Set the filter. In the example below, I am searching for TDX files. Then, I can perform whichever operations I want (e.g. copy, delete, rename).
vb Code:
For Each filetdx As String In IO.Directory.GetFiles("MyDirectory", "*.TDX")
' perform your operation here
Next
Re: Copy Files using wildcards
Can you help me on my code, its still not working. In Textbox 8 i am typing *.XML and it still doesn't copy anything. And there is a .XML file in the app startup directory.
Whats wrong with it?
VB.NET Code:
For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
My.Computer.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, foundFile))
Next
Re: Copy Files using wildcards
This is one of those cases where you don't actually know what your code is doing because you haven't actually checked. You're using data and assuming that it's correct without actually checking whether it's correct. If the code isn't working then something isn't right, so the first thing you should do is check the data. Look at your code:
Code:
My.Computer.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, foundFile))
Do you know what those two highlighted parts actually evaluate to? I'll wager not, because they are undoubtedly not producing what you think they are. You could have found that out for yourself in several easy ways, including this:
vb Code:
MessageBox.Show(IO.Path.Combine(Application.StartupPath, foundFile))
MessageBox.Show(IO.Path.Combine(Fullpath, foundFile))
Re: Copy Files using wildcards
the fact is that my code wont even get to that part,
the full section of code to do with it
vb Code:
If TextBox8.Text.Contains("*") Then
Try
For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
MsgBox("App Startup + Textbox" & IO.Path.Combine(Application.StartupPath, TextBox8.Text))
Next
Catch ex As Exception
MsgBox("Couldn't pack files from 7", MsgBoxStyle.Critical)
End Try
End If
It just says Couldn't pack files from 7 which is meaning its throwing an exception.
Re: Copy Files using wildcards
Ok i found out that it is finding the files and it is the correct path to be copied, so now cna you help me please.
vb.net Code:
If TextBox8.Text.Contains("*") Then
Try
For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
MsgBox(IO.Path.Combine(Application.StartupPath, foundFile))
Next
Catch ex As Exception
MsgBox("Couldn't pack files from 7", MsgBoxStyle.Critical)
End Try
End If
There is 1 .xml file in the app startup directory and it only shows that msgbox once so i know its finding it. Also path it gave was C:\Users\Aaron T\Documents\Visual Studio 2010\Projects\Project Packer\Project Packer\bin\Debug\*.xml
And from when ive been searching on the web to find out how to do this im pretty sure that you just add the *.xml (filetype) to use wildcards.
Re: Copy Files using wildcards
Ok ive fixed that part, it was because for some reason in the IO.Path.Combine(Fullpath, foundFile) it was giving the full path of foundFile, so i used a little function
vb.net Code:
For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
FileIO.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, foundFile), IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
Next
Public Function GetFileNameFromPath(ByVal Path As String) As String
Try
Return Path.Substring(Path.LastIndexOf("\") + 1)
Catch ex As Exception
Return Path
End Try
End Function
BUT---------------
its not copying the file for some reason,
and the path given from this:
Code:
MsgBox(IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
is:
Code:
C:\Users\Aaron T\AppData\Roaming\Project_Packer\Project Packer\1.0.0.0\Packed Files - 234h1\Project Packer.xml
Re: Copy Files using wildcards
Fixed it,
Full code:
VB.NET Code:
For Each foundFile As String In IO.Directory.GetFiles(Application.StartupPath, TextBox8.Text)
FileIO.FileSystem.CopyFile(IO.Path.Combine(Application.StartupPath, GetFileNameFromPath(foundFile)), IO.Path.Combine(Fullpath, GetFileNameFromPath(foundFile)))
Next
FOR FUTURE REFERENCE:
Code Snippet
Code:
For Each foundFile As String In IO.Directory.GetFiles("DIRECTORY", "WILDCARD")
FileIO.FileSystem.CopyFile(IO.Path.Combine("PATH-TO-COPY-FROM", GetFileNameFromPath(foundFile)), IO.Path.Combine("PATH-TO-COPY-TO", GetFileNameFromPath(foundFile)))
Next