Resources » Code Snippets » File Operations

Moving files from one folder to another


Posted Date: 05-Mar-2007  Last Updated:   Category: File Operations    
Author: Member Level: Gold    Points: 10


This article explain about how to Moving files from one folder to another with the code snippet in simple way.



The following c# code shows how to move all the files in one folder to another. The program can be customized to move selected files too


DirectoryInfo dir1= new DirectoryInfo("F:Folder1");
DirectoryInfo dir2 = new DirectoryInfo("F:Folder2");

FileInfo[] Folder1Files = dir1.GetFiles();
if(Folder1Files.Length > 0)
{
foreach(FileInfo aFile in Folder1Files)
{
if(File.Exists("F:Folder2" + aFile.Name))
{
File.Delete("F:Folder2" + aFile.Name);
}
aFile.MoveTo("F:Folder2" + aFile.Name);
}
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Moving files from one folder to another"
Author: Yuvraj Nipankar    27 Dec 2007Member Level: Bronze   Points : 0
Thanks
Mathews . T. Varghese
ArvinMeritor

your code is simple and worth i also reached to the same code but i found trouble in the line
FileInfo[] Folder1Files = dir1.GetFiles();
my system is showing some err or warning about conversion of fileinfo when i use filters but now with ur code it is simple i modified the code for my requirement of getting a specefic files

FileInfo[] Folder1Files = dir1.GetFiles(filter4Files);

and it solve my trouble.

Regarding the Page it is simple and to the point.



Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil



Author: revathy    14 Jul 2009Member Level: Bronze   Points : 1
hi,
I am getting the below error in the line
FileInfo[] Folder1Files = dir1.GetFiles();
{"Logon failure: unknown user name or bad password.\r\n" }
Please help me to resolve this.



Author: revathy    14 Jul 2009Member Level: Bronze   Points : 0
Thanks a lot.YOur code is very simple and superb.
Its working fine.



Guest Author: Ajay     24 Aug 2012
Simple Superb ......nice !!!


Guest Author: Chaitanya     11 Sep 2013
Really good article


Author: Faiz Ahmed    12 Feb 2014Member Level: Bronze   Points : 2
Dear kapil....
Greetings, your code is very simple and worth but i have complexity that how to select a particular file or group of files (to be specific images) to transfer from one folder to another.
Please Contribute....
Thanks in Advance.

Regards,
F@!Z



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India