rinkuchoudhury 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.
The quick answers from and work for a simple, flat copy. For a more robust, production-ready approach the following covers common gaps: create the destination folders, build paths with Path.Combine (avoid manual "\" concatenation), optionally overwrite, handle errors (don’t swallow them), and support subfolders. In ASP.NET scenarios the worker identity must have read/write permission and virtual paths should be resolved to physical paths before copying.
Example VB.NET routine that copies a directory recursively and returns failures for logging:
Imports System.IO
Public Shared Function CopyDirectoryRecursive(sourceDir As String, destDir As String, Optional overwrite As Boolean = False) As List(Of String)
Dim failures As New List(Of String)()
If Not Directory.Exists(sourceDir) Then
failures.Add("Source not found: " & sourceDir)
Return failures
End If
Directory.CreateDirectory(destDir)
For Each srcFile In Directory.EnumerateFiles(sourceDir)
Dim destFile = Path.Combine(destDir, Path.GetFileName(srcFile))
Try
File.Copy(srcFile, destFile, overwrite)
Catch ex As Exception
failures.Add(srcFile & " -> " & ex.Message)
End Try
Next
For Each srcSub In Directory.EnumerateDirectories(sourceDir)
Dim destSub = Path.Combine(destDir, Path.GetFileName(srcSub))
failures.AddRange(CopyDirectoryRecursive(srcSub, destSub, overwrite))
Next
Return failures
End Function Notes and troubleshooting:
For Each f In Directory.GetFiles(sourcefolderpath)
file.copy(f,destination path)
Next
thank you
hi
I am working in asp.net,vb.net
I have to copy all files from 1 folder to another folder through coding using system.iothe files in the new folder should remain with the same name
how to do this.
--------------------------------------------------------------------------------
Here is VB code
imports System.io
Public Shared Sub CopyFlashScriptFile(ByVal SourceDirectory As String, ByVal DestinationDirectory As String)
Try
Dim f() As String = Directory.GetFiles(SourceDirectory)
For i As Integer = 0 To UBound(f)
File.Copy(f(i), DestinationDirectory & "\" & fileNameWithoutThePath(f(i)))
Next
Catch ex As Exception
End Try
End Sub We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.