Click here to Skip to main content
15,902,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a directory "C:\Ajith\aaaa" inside this aaaa folder i have two pdf files. I need to create two folders as 'A' and 'b' and move each .pdf files into them. how can i do this. pls somebody help using c$ .net ..thanks in advance.
Posted

Hi,

see this code as an example:
C#
string root = "C:\\Ajith\\aaaa";
System.IO.Directory.CreateDirectory(root + "\\A");
System.IO.Directory.CreateDirectory(root + "\\B");
System.IO.File.Move(root + "\\test1.pdf", root + "\\A\\test1.pdf");
System.IO.File.Move(root + "\\test2.pdf", root + "\\B\\test2.pdf");

To get the list of all files in your directory you could use this code:
C#
string [] files = System.IO.Directory.GetFiles(root);
 
Share this answer
 
v2
Hi,

See the below link which help you how to create folder.

http://www.venkateswarlu.co.in/articles/DotNet/Creat_Delete_Folder.aspx[^]

and see the below link which help you to copy file from one folder to another folder.

http://msdn.microsoft.com/en-us/library/cc148994.aspx[^]
http://www.dreamincode.net/forums/topic/113204-how-to-copy-a-file-from-place-to-another/[^]

Thanks,
Viprat
 
Share this answer
 
Try this
C#
string path = @"C:\Ajith\aaaa\MyTest.pdf";
        string path2 = @"C:\Ajith\aaaa\A\MyTest.pdf";

if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created, 
                // but the handle is not kept. 
                using (FileStream fs = File.Create(path)) {}
            }

            System.IO.Directory.CreateDirectory("C:\Ajith\aaaa\A");

            // Move the file.
            File.Move(path, path2);
 
Share this answer
 
v2
//Create Dir//
C#
Use System.IO.Directory.CreateDirectory("D:\ad");


//For Move All Files, Use File Copy Algo//
-----------
else
//File Copy//
C#
System.IO.File.Copy("C:\Ajith\aaaa\hi.pdf","D:\hi.pdf");
 
Share this answer
 
v2
Hi friend,

This will help You


String l_sDirectoryName = "C:\\Consolidated";
     DirectoryInfo l_dDirInfo = new DirectoryInfo(l_sDirectoryName);
     if(l_dDirInfo.Exists ==false )
           Directory.CreateDirectory(l_sDirectoryName);
       List<String> MyMusicFiles = Directory.GetFiles("c:\\Music", "*.*", SearchOption.AllDirectories).ToList();
       foreach (string file in MyMusicFiles)
       {
           FileInfo mFile = new FileInfo(file);
           if(new FileInfo(l_dDirInfo +"\\"+ mFile.Name).Exists==false)//to remove name collusion
                mFile.MoveTo(l_dDirInfo +"\\"+ mFile.Name);
       }



Other wise check the following link:

http://msdn.microsoft.com/en-us/library/cc148994.aspx[^]
 
Share this answer
 
v2

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