neuron Administrator
Posts : 38 Join date : 2009-08-06
| Subject: Copy from 1 folder to another in VB.NET Thu Aug 06, 2009 11:47 pm | |
| Sample code to copy from one folder to another regardless of file location within the network.. - Code:
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If CopyDirectory("L:\XXXXX,TXT", "C:\XXXXX.TXT") Then MessageBox.Show("Copied successfully!!") End Else MessageBox.Show("Error copying directory") End If End Sub
Public Function CopyDirectory(ByVal Src As String, ByVal Dest As String, Optional _ ByVal bQuiet As Boolean = False) As Boolean If Not Directory.Exists(Src) Then Throw New DirectoryNotFoundException("The directory " & Src & " does not exists") End If If Directory.Exists(Dest) AndAlso Not bQuiet Then If MessageBox.Show("directory " & Dest & " already exists." & vbCrLf & _ "If you continue, any files with the same name will be overwritten", _ "Continue?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button2) = DialogResult.Cancel Then Exit Function End If
'add Directory Seperator Character (\) for the string concatenation shown later If Dest.Substring(Dest.Length - 1, 1) <> Path.DirectorySeparatorChar Then Dest += Path.DirectorySeparatorChar End If If Not Directory.Exists(Dest) Then Directory.CreateDirectory(Dest) Dim Files As String() Files = Directory.GetFileSystemEntries(Src) Dim element As String For Each element In Files If Directory.Exists(element) Then 'if the current FileSystemEntry is a directory, 'call this function recursively CopyDirectory(element, Dest & Path.GetFileName(element), True) Else 'the current FileSystemEntry is a file so just copy it File.Copy(element, Dest & Path.GetFileName(element), True) End If Next Return True End Function
or you can try this one - Code:
-
Dim appath As String Dim FileToCopy As String Dim NewCopy As String Dim DateString As String = Format(Now, "yyyy-MMM-d-hhmmss")
appath = Application.StartupPath()
FileToCopy = appath & "\SISdatabase.mdb" NewCopy = "C:\SISdatabase-" & DateString & ".mdb"
If System.IO.File.Exists(FileToCopy) = True Then System.IO.File.Copy(FileToCopy, NewCopy) MsgBox("File Copied at " & NewCopy) End If
| |
|