Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
2.43/5 (5 votes)
See more:
how to Automatically create directories from inpur path to get files?
Posted
Comments
Md. Khaled Hussain 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 18-Oct-12 6:28am    
what??? you want to create a directory or you want to get files from the directory?
NehaShetty 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 18-Oct-12 7:04am    
so you wanted to created same structure of folders in output path?
NehaShetty 18-Oct-12 7:05am    
Yes.

C#
if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }


Thanks
 
Share this answer
 
C#
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)); 
            } 
        }
 
Share this answer
 
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);
}
}
 
Share this answer
 
C#
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();
    }
}
 
Share this answer
 
Comments
SoMad 2-Apr-13 4:08am    

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