0

I have a CSV file which is generated everyday I wanted to move that csv file into different folder with today's date.

my CSV file finaltest12.csv

This is my code:

if (System.IO.File.Exists(@"F:/Explor/final test/finaltest12.csv"))
{
    String Todaysdate=DateTime.Now.ToString("dd-MMM-yyyy");
    if(!Directory.Exists("I:\\test\\final test\\snaps\\"+Todaysdate)
    {
         Directory.CreateDirectory("I:\\test\\final test\\snaps\\"+Todaysdate); 
    } 

}
H. Mahida
  • 2,356
  • 1
  • 12
  • 23

2 Answers2

2

To move a file you can use File.Move(..)

        string sourceFile = @"c:\finaltest12.csv";
        if (!File.Exists(sourceFile))
            return;

        string Todaysdate = DateTime.Now.ToString("dd-MMM-yyyy");
        string newPath = Path.Combine(@"c:\test\final test\snaps\", Todaysdate);

        if (!Directory.Exists(newPath))
            Directory.CreateDirectory(newPath);

        try
        {
            File.Move(sourceFile, Path.Combine(newPath, Path.GetFileName(sourceFile)));
        }
        catch
        {
            //ToDo
        }
  • You are wellcome! BTW [string vs. String](http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string). Usually in c# string is used instead of String. –  Sep 16 '14 at 11:24
1

you need following code it's working try this

if (System.IO.File.Exists(@"D:/finaltest12.csv"))
        {
            string fileoldPath="D:\\finaltest12.csv";
        string Todaysdate ="E:\\";
        bool isExists = System.IO.Directory.Exists(Todaysdate);
        if (!isExists)
            System.IO.Directory.CreateDirectory(Todaysdate);

        System.IO.File.Move(fileoldPath, Todaysdate+"\\finaltest12.csv");


        }