Click here to Skip to main content
15,886,661 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have set of text files in folder. I want to copy this folders into another folder with name and datatime.

C#
string directory = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\SampleData";

string comdir = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\CompletedData";
string[] fiels = System.IO.Directory.GetFiles(directory);
foreach (string s in fiels)
            {
               
                string st = DateTime.Now.ToShortDateString();
               string  fileName = System.IO.Path.GetFileName(s)+st;
               string  destFile = System.IO.Path.Combine(comdir, fileName);
               System.IO.File.Copy(s, destFile, true);
            
            }


But i got "The given path's format is not supported" this error.

Anyone help me to solve this?
Posted
Comments
Karen Mitchelle 9-May-14 5:25am    
It is because, the Date is in mm/dd/yy.
As far as I know, "/" is not allowed when naming a file.
vinodhini sekar 9-May-14 5:32am    
Ok Thank you. But i want save with date and time. what can i do for this?
Karen Mitchelle 9-May-14 5:34am    
Check out my answer. Although, it's just a date. I haven't tried saving file with the time yet.

try using this...

C#
string st = Date.Now.Year + Date.Now.Month + Date.Now.Day //outputs the date without "/"


I tried it in VB.Net. It works well.
 
Share this answer
 
Hmmm... Small changes have been done:
C#
string directory = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\SampleData";
string comdir = @"C:\Documents and Settings\vinodhini\Desktop\Merge Data\CompletedData\" + DateTime.Now.ToString("yyyyMMdd") + "\";

string[] fiels = System.IO.Directory.GetFiles(directory);
foreach (string s in fiels)
            {
               string  fileName = System.IO.Path.GetFileName(s);
               string  destFile = System.IO.Path.Combine(comdir, fileName);
               System.IO.File.Copy(s, destFile, true);
            }
 
Share this answer
 
Thank you i solve myself
string dt = DateTime.Now.ToString("dd-MM-yyyy-hh-mm");
 
Share this answer
 

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