Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0
how to Automatically create directories from inpur path to get files?
Posted 18-Oct-12 1:21am
Comments
Md. Khaled Hussain at 18-Oct-12 6:28am
   
Do you want to know whether it is possible to do without executing any code? Simplest way should be "System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(input));"
Sushil Mate at 18-Oct-12 6:28am
   
what??? you want to create a directory or you want to get files from the directory?
ullalanjali at 18-Oct-12 6:46am
   
For a given input path the submitted action should perform in such a way that the output path also should have to create the same folders and sub-folders similar to input path.
Sushil Mate at 18-Oct-12 7:04am
   
so you wanted to created same structure of folders in output path?
ullalanjali at 18-Oct-12 7:05am
   
Yes.
Sushil Mate at 18-Oct-12 7:06am
   
& even you wanted to copy the files too from input folder to output folder?
ullalanjali at 18-Oct-12 7:15am
   
yes.
Sushil Mate at 18-Oct-12 7:18am
   
check my solution
ullalanjali at 18-Oct-12 7:25am
   
ya, suppose if the path is "E:\Test" and inside "Test" folder there are sub folders like "Dir1","Dir2", then it must carry these 2 directories and create in output path is "C:\Test1" must be changed to "C:\Test1\Dir1"& "C:\Test1\Dir2"
Sushil Mate at 18-Oct-12 7:27am
   
yah it will work.. try in your application.. just src will be your inputpath & dest will be your output path.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

DirectoryInfo src = new DirectoryInfo(@"E:\Test\Dir1"); 
DirectoryInfo dest = new DirectoryInfo(@"C:\Dir2"); 
CopyDirectory(src, dest); 
 
 
static void CopyDirectory(DirectoryInfo source, DirectoryInfo destination) 
        { 
            if (!destination.Exists) 
            { 
                destination.Create(); 
            } 
 
            // Copy all files. 
            FileInfo[] files = source.GetFiles(); 
            foreach (FileInfo file in files) 
            { 
                file.CopyTo(Path.Combine(destination.FullName, 
                    file.Name)); 
            } 
 
            // Process subdirectories. 
            DirectoryInfo[] dirs = source.GetDirectories(); 
            foreach (DirectoryInfo dir in dirs) 
            { 
                // Get destination directory. 
                string destinationDir = Path.Combine(destination.FullName, dir.Name); 
 
                // Call CopyDirectory() recursively. 
                CopyDirectory(dir, new DirectoryInfo(destinationDir)); 
            } 
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

public void Getpr()
{
DateTime dt = DateTime.Now;
 
string[] files = Directory.GetFiles("file Name", "*.*", SearchOption.AllDirectories);
int checkCount = files.Length;
foreach (string file in files)
{
//wait for free limit...
while (workingCounter >= workingLimit)
{
Thread.Sleep(100);
}
workingCounter += 1;
ParameterizedThreadStart pts = new ParameterizedThreadStart(ProcessFile);
Thread th = new Thread(pts);
th.Start(file);
}
//wait for all threads to complete...
while (processedCounter < checkCount)
{
Thread.Sleep(100);
}
 
DateTime dt1 = DateTime.Now;
TimeSpan t = dt1 - dt;
MessageBox.Show("Work completed!" + t.TotalMinutes.ToString() + " ");
}
 
public void ProcessFile(object file)
{
try
{
string strpath = Path.GetRandomFileName();
if (!Directory.Exists("Directory name" + strpath))
{
Directory.CreateDirectory("Directory name" + strpath);
}

//make some sleep for demo...
Thread.Sleep(1000);
}
catch (Exception ex)
{
//handle your exception...
string exMsg = ex.Message;
}
finally
{
Interlocked.Decrement(ref workingCounter);
Interlocked.Increment(ref processedCounter);
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

public class CreateFileOrFolder
{
    static void Main()
    {
        // Specify a "currently active folder"
        string activeDir = @"c:\testdir2";
 
        //Create a new subfolder under the current active folder
        string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
 
        // Create the subfolder
        System.IO.Directory.CreateDirectory(newPath);
 
        // Create a new file name. This example generates
        // a random string.
        string newFileName = System.IO.Path.GetRandomFileName();
 
        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(newPath, newFileName);
 
        // Create the file and write to it.
        // DANGER: System.IO.File.Create will overwrite the file
        // if it already exists. This can occur even with
        // random file names.
        if (!System.IO.File.Exists(newPath))
        {
            using (System.IO.FileStream fs = System.IO.File.Create(newPath))
            {
                for (byte i = 0; i < 100; i++)
                {
                    fs.WriteByte(i);
                }
            }
        }
 
        // Read data back from the file to prove
        // that the previous code worked.
        try
        {
            byte[] readBuffer = System.IO.File.ReadAllBytes(newPath);
            foreach (byte b in readBuffer)
            {
                Console.WriteLine(b);
            }
        }
        catch (System.IO.IOException e)
        {
            Console.WriteLine(e.Message);
        }
 
        // Keep the console window open in debug mode.
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
  Permalink  
Comments
SoMad at 2-Apr-13 4:08am
   

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

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 BillWoodruff 173
3 PIEBALDconsult 160
4 CPallini 155
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 18 Dec 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100