How to copy a particular file into a folder and all its sub folders

I have a notepad file and I want to copy it to one folder adn all its subfolders. How do I do that?
TheCommunicatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lee555J5Commented:
Check out this link to create a batch file
http://techrepublic.com.com/5208-6230-0.html?forumID=35&threadID=34194&messageID=207765&tag=content;leftCol
Check out this link for the DOS utility SWEEP (scan before you use)
http://users.csc.calpoly.edu/~bfriesen/software/console.shtml
Lee


0
TribmosCommented:
C# code here; give it a filename and a destination directory and it recursively will put that file in the supplied directory and every sub directory.

Does not have error handling for access violations
public void CopyToFolders(string filename, string directory)
        {
            //Copy the file to the current directory
            System.IO.FileInfo finfo = new System.IO.FileInfo(filename);
            finfo.CopyTo(System.IO.Path.Combine(directory, finfo.Name));

            //Get current directory
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory);
            //get list of directories in there
            System.IO.DirectoryInfo[] dFolders = di.GetDirectories();
            //Recursive call for each sub directory
            foreach (System.IO.DirectoryInfo d in dFolders)
            {
                CopyToFolders(filename, d.FullName);
            }

        }

Open in new window

0
TheCommunicatorAuthor Commented:
what does
System.IO.FileInfo do? because it is giving me an error here..
0
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

TheCommunicatorAuthor Commented:
Hi Tribmos,
hen you say :
" System.IO.FileInfo finfo = new System.IO.FileInfo(filename); " copies the files to the current directory, what do we mean by the current directory in that case? because I have that file residing at my desktop.

0
arthur_infoCommented:
I'll try to comment better the code:

What's the error that are you seeing?
public void CopyToFolders(string filename, string directory) 
        { 
            //Gets information about the file (file name, file path, etc.... 
            System.IO.FileInfo finfo = new System.IO.FileInfo(filename); 
//Copy the file to the path directory+name_of_the_file (finfo.Name)
            finfo.CopyTo(System.IO.Path.Combine(directory, finfo.Name)); 
 
            //Get information about the current directory
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory); 
            //get list of directories in there 
            System.IO.DirectoryInfo[] dFolders = di.GetDirectories(); 
            //Recursive call for each sub directory 
            foreach (System.IO.DirectoryInfo d in dFolders) 
            { 
                CopyToFolders(filename, d.FullName); 
            } 
 
        }

Open in new window

0
Shahan AyyubSenior Software EngineerCommented:
Try this one:

using System.IO;        /// include this namespace also

         static void Main(string[] args)
        {
            // you can either directly set these two strings or get path from somewhere.
            string SourceFile = @"C:\Documents and Settings\Shahan\Desktop\abc.txt";
            string DestFolder = @"D:\testfolder";

            // Getting All Directories, making tree like structure.
            string[] Dir = Directory.GetDirectories(DestFolder,"*",SearchOption.AllDirectories);
               
            // Extracting Filename
            string[] Filename= SourceFile.Split('\\')  ;  
           
            //Copying SourceFile, here 'location' is a 'Folder' not 'File' so concatenation required.
            foreach (string location in Dir)
            {
                File.Copy(SourceFile, location + "\\" + Filename[Filename.Length-1]);  /// copies in sub-Directories only              
            }
            File.Copy(SourceFile, DestFolder + "\\" + Filename[Filename.Length-1]); // Copy in given Directory also.                
        }
0
Shahan AyyubSenior Software EngineerCommented:
i have mad this directory before, you can make changes acc. to your wish. My area of consideration is :

"   // Getting All Directories, making tree like structure. " 
0
TheCommunicatorAuthor Commented:
@ finfo.CopyTo(System.IO.Path.Combine(directory, finfo.Na me));

it gives me error that "Could not find "filename.txt", which is the file I am trying to assign to Finfo. Now, it is looking for the file in the default project directory while my file resides on the desktop. I can understand that when nothing is specified then it will look for the file in the default project directory but my question is that instead of System.IO.FileInfo finfo = new System.IO.FileInfo(filename); , do I have any more detailed way that I can specify file address along with the file name?
 
0
lee555J5Commented:
I just tested the 32-bit version of sweep on 32-bit and 64-bit Win7, and it worked perfectly.
Simply,
  1. Put sweep.exe into the folder
  2. Open a command window to this folder
  3. Run sweep copy path\file and your file is copied to this folder and all its subfolders
Is this not what you are looking for?
Lee
0
leonov_alexCommented:
Shorter way:

private void CopyToAllFolders(string file, string dir)
{
    FileInfo fi = new FileInfo(file);
    File.Copy(file, Path.Combine(dir, fi.Name));
    foreach (string subdir in Directory.GetDirectories(dir, "*.*", SearchOption.AllDirectories))
        File.Copy(file, Path.Combine(subdir, fi.Name));
}
0
TribmosCommented:
The file you specify (full path) needs to exist

example
 CopyToFolders(@"C:\test_docs\test.txt", @"C:\test_folders");

System.IO.FileInfo   is a class that will peek at the file you specify and can pull information about it.  In this case looking at the name  (ignoring path information) so that it can combine that info with the current directory and produce a valid path


You can additionally check....

public void CopyToFolders(string filename, string directory)
        {
            if (!System.IO.File.Exists(filename))
            {
                System.Diagnostics.Debug.WriteLine("File " + filename + " does not exist.");
                return;
            }
            //obtain file information about the file to copy
            System.IO.FileInfo finfo = new System.IO.FileInfo(filename);
            //Copy said file to the current directory specified
            finfo.CopyTo(System.IO.Path.Combine(directory, finfo.Name));

            //Get current directory information
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory);
            //get list of directories in the current directory
            System.IO.DirectoryInfo[] dFolders = di.GetDirectories();
            //Recursive call for each sub directory
            foreach (System.IO.DirectoryInfo d in dFolders)
            {
                CopyToFolders(filename, d.FullName);
            }
        }

Open in new window

0
TribmosCommented:
On second thought to cover more cases if the file and directory exists.


public void CopyToFolders(string filename, string directory)
        {
            if (!System.IO.File.Exists(filename))
            {
                System.Diagnostics.Debug.WriteLine("File " + filename + " does not exist.");
                return;
            }
            if (!System.IO.Directory.Exists(directory))
            {
                System.Diagnostics.Debug.WriteLine("Directory " + directory + " does not exist.");
                return;
            }
            //obtain file information about the file to copy
            System.IO.FileInfo finfo = new System.IO.FileInfo(filename);
            //Copy said file to the current directory specified
            finfo.CopyTo(System.IO.Path.Combine(directory, finfo.Name));

            //Get current directory information
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(directory);
            //get list of directories in the current directory
            System.IO.DirectoryInfo[] dFolders = di.GetDirectories();
            //Recursive call for each sub directory
            foreach (System.IO.DirectoryInfo d in dFolders)
            {
                CopyToFolders(filename, d.FullName);
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TribmosCommented:
Maybe I should of used "file path" instead of "filename" for the variable; My apologies, i figured that would be a trivial matter.  Same for directory.  You want the full path to both of those locations.

ex  "C:\Documents and Settings\User\Desktop\my_file.txt"
or..
"C:\windows\system32\"
etc etc

just putting in "my_file.txt" the software will have no idea where the file is you are trying to locate.
0
TheCommunicatorAuthor Commented:
Hello guys,
                    I am so sorry for the late reply. Actually I was really busy with some stuff. Well, my solution actually was achieved when Tribmos posted his/her second solution but due to some time constraints, I could not come and update you guys. But as everyone has invested time in helping me get my solution. I think I will give everybody some points.
I hope tribmos does not mind this. :).
 
 
 
 
 
0
TheCommunicatorAuthor Commented:
I am so sorry fo rthis confusion guys. I hope every body would nto mind this time.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.