Create folder on every console usage

2

I got a program ready on the internet to create a folder, so I tried to adapt it for me but I can not do it.

I would like every time I run it, create a subfolder inside the main with a different name, and let that name be the day I was using, so if I used the 2x program the same day it did not delete the first folder just be as if it were double ex 19/09 ... 19/09 (2).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class CreateFileOrFolder
{
    static void Main()
    {
        // Specify a "currently active folder"
        string activeDir = @"B:\Quality\QAS\FOTOS DO FERRAMENTAL";

        //Create a new subfolder under the current active folder
        string newPath = System.IO.Path.Combine(activeDir, "19/09/2017");

        // 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();

        // Combina o arquivo com o caminho
        newPath = System.IO.Path.Combine(newPath, newFileName);

        // Criar arquivo e sobrescrever ele.
        // DANGER: System.IO.File.Create will overwrite the file
        // Se já existe ele pode ocorrer a criação de arquivos random
        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);
                }
            }
        }

        // Ler a data de volta para provar 
        // se o código anterior funciona.
        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);
        }

        // manter o código aberto no debug.
        System.Console.WriteLine("Diretorio criado com sucesso, Pressione qualquer tecla para iniciar o programa de captura.");
        System.Console.ReadKey();
    }
}
    
asked by anonymous 19.09.2017 / 17:48

2 answers

1

If the name is the same it will not be created and will not delete anything, nothing will be done, nor will it give error.

I've improved a bit, but this code is still bad.

using System;
using static System.Console;
using System.IO;

public class CreateFileOrFolder {
    public static void Main() {
        var activeDir = @"B:\Quality\QAS\FOTOS DO FERRAMENTAL";
        string newPath = Path.Combine(activeDir, DateTime.Now.ToString("yyyyMMdd"));
        Directory.CreateDirectory(newPath);
        newPath = Path.Combine(newPath, Path.GetRandomFileName());
        if (!File.Exists(newPath)) { //isto pode dar condição de corrida, mas vou deixar
            using (FileStream fs = File.Create(newPath)) {
                for (byte i = 0; i < 100; i++) {
                    fs.WriteByte(i); //isto é lento pra bedéu
                }
            }
        }
        byte[] readBuffer = File.ReadAllBytes(newPath);
        foreach (byte b in readBuffer) {
            WriteLine(b);
        }
        WriteLine("Diretorio criado com sucesso, Pressione qualquer tecla para iniciar o programa de captura.");
    }
}

See running on .NET Fiddle (does not run for security). And no Coding Ground . Also I placed GitHub for future reference .

    
19.09.2017 / 18:08
2

To not delete the folder and create a pasta(2) , see this function:

void CriarPasta (string path)//Cria a função
{
    if (Directory.Exists(path)) //Verifica se já existe uma pasta com o mesmo nome
    {
        for (int i = 0; !Directory.Exists((path + "(" + i + ")")); i ++)//Verifica se exste uma pasta com o nome + (i)
        {
            Directory.CreateDirectory(path + "(" + i + ")");//Cria a pasta
        }
    }
    else // se não
      Directory.CreateDirectory(path);//Cria a pasta
}
    
19.09.2017 / 18:05