Click here to Skip to main content
15,507,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I have a folder of text files that I want to copy to another folder. Straight forward vb copy. When I step through the code, no problem, they copy.

When I run the exe, only copies 1 file.

Has anyone came across this problem. The version of vb is visual studio 2013. I would ask if there is another way to copy but I have tried and get the same problem.

Must be something I am doing wrong.


What I have tried:

Dim sSourceDir As String = "C:\Source\"
        Dim sTargetDir As String = "C:\Target\"        
Dim sTextList As String() = Directory.GetFiles(sSourceDir, "*.txt")

        'copy text files
        For Each f As String In sTextList
            'Remove path from the file name.
            Dim fName As String = f.Substring(sSourceDir.Length)

            ' Use the Path.Combine method to safely append the file name to the path.
            ' Will overwrite if the destination file already exists.
            File.Copy(Path.Combine(sSourceDir, fName), Path.Combine(sTargetDir, fName), True)

            '    'Make sure the file is copied, 10 second delay.
Updated 17-Mar-17 19:54pm
[no name] 17-Mar-17 12:43pm    
Nothing is jumping out at me but I would be suspicious of "f.Substring(sSourceDir.Length)"
Richard MacCutchan 17-Mar-17 12:46pm    
You should display some information about progress so you can see exactly what is happening. It's impossible to guess from here.
Richard Deeming 17-Mar-17 13:01pm    
Replace f.Substring(...) with Path.GetFileName(f), just in case.

And there's no need for the Thread.Sleep call - File.Copy doesn't return until the copy has completed.

1 solution

I have a function in C# to copy files from one directory to other directory.
May be it can help you.

public List<string> CopyFiles(string sourceDirectory, string destinationDirectory)
           List<string> CopiedFiles = new List<string>();

           //Get the files from source directory to copy them on new location
           string[] sourceFiles = GetFilesInDirectory(sourceDirectory);

           //Make sure the destination directory exists.
           if (!DirectoryExists(destinationDirectory))

           int ImageCounter = 0;
           while (ImageCounter < sourceFiles.Length)
               FileInfo SourceFile = new System.IO.FileInfo(sourceFiles[ImageCounter]);
               string imagepath = destinationDirectory + "/" + SourceFile.Name;

               //Check for valid extention.
               bool isValidExtnsion = false;
               Array itemValues = System.Enum.GetValues(typeof(ValidImageExtensions));
               int i = 0;
               while (i < itemValues.Length)
                   if (SourceFile.Extension.ToLower() == "." + itemValues.GetValue(i))
                       isValidExtnsion = true;


               //Copy file with valid extensions.
               if (isValidExtnsion)
                   //if (!FileExists(imagepath))
                       SourceFile.CopyTo(imagepath, true);

           return CopiedFiles;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900