[Help] Copying multiple files with the same extension.

Posts 1–6 of 6 · Page 1 of 1
[Help] Copying multiple files with the same extension.
Hey guys, i'm currently using this code to try and copy multiple files of the ".txt" extension from one place to another, the only problem is I'm getting this error every time.



My code:

[php]
For Each FileName As String In Directory.GetFiles(TextBox1.Text & "*.txt")
File.Copy( _
FileName, _
Path.Combine((TextBox1.Text & "\Backup cleaner files"), Path.GetFileName(FileName)) _
)
Next FileName
[/php]

Probably something really simple but I'm guessing that it's the '*' that's the illegal character...but I can't get rid of that.
For Each x As String In My.Computer.FileSystem.GetFiles(textbox1.text,File IO.SearchOption.SearchAllSubDirectories, "*.txt")
My.Computer.FileSystem.CopyFile(x,Path.Combine((Te xtBox1.Text & "\Backup cleaner files\"),Path.GetFileName(FileName)))
Next


Tested and working
Quote Originally Posted by Hassan View Post
For Each x As String In My.Computer.FileSystem.GetFiles(textbox1.text,File IO.SearchOption.SearchAllSubDirectories, "*.txt")
My.Computer.FileSystem.CopyFile(x,Path.Combine((Te xtBox1.Text & "\Backup cleaner files\"),Path.GetFileName(FileName)))
Next


Tested and working

Says FileName is not declared (highlighted in red)
Code:
Imports System.IO
Code:
Private Sub opendb(ByVal folderpath As String, ByVal fileExtension As String)
        Dim dr As New System.IO.DirectoryInfo(folderpath) ' Dynamically it would be your textbox folder path or whatever 
        Dim finfo As System.IO.FileInfo() = dr.GetFiles()
        For Each fi As System.IO.FileInfo In finfo
            If fi.Extension.ToUpper() = fileExtension.ToUpper() Then
                fi.Delete()
            End If
        Next
    End Sub
Code:
opendb("C:\", ".txt")
This is actually to delete the files. Rewrite it so it matches
Thanks guys, got it working from FLAMESABER's code:

[php]
For Each x As String In My.Computer.FileSystem.GetFiles(TextBox1.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.txt")

My.Computer.FileSystem.CopyFile(x, Path.Combine((TextBox1.Text & "\Backup cleaner files\"), Path.GetFileName(x)))
Next
[/php]
I copy pasted your second line, because it was ok. Didn't noticed that the filename variable was undeclared. Glad to see that worked for you now.
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us