Link to home
Start Free TrialLog in
Avatar of lobos
lobos

asked on

duplicate a text file

In my windows service I am creating a text file (filename is the current date) and dumping it into a specific folder. From there I want to duplicate that first file keep the original and create a new one with a specific name (lets call it copyoffile).
>>>>From there I want to copy that file from one folder to another (but thats not the question here).<<<<

So how do I duplicate the text file, assign it to a name of copyoffile.txt while maintaining the first copy of the file as well. Then the next time the service runs this (again the first file is created with the current date and time as the filename, but again the file is to be duplicated, called copyoffile.txt, but this file will and should overwrite the file from the previous time the service was run.
Any ideas?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Example:
System.IO.File.Copy(@"C:\Temp\Test.xml", @"C:\Temp\Copy of Test.xml");

Bob
Avatar of lobos
lobos

ASKER

Thanks but I am getting error message...

System.IO.File.Copy(@LogFile, @"C:\Temp\dd.txt");

The process cannot access the file "C:\Temp\dd.txt" because it is being used by another process.
That usually happens if you are creating the file, and not closing the file handle.  What creates the file in the first place?

Bob
Avatar of lobos

ASKER

LogFile = FlatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";

using (FileStream fs = File.Create(LogFile))
{
DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS);

for ( int x=0; x<dsFlat.Tables[0].Rows.Count;x++)
{                                    
string str = DateTime.Parse(dsFlat.Tables[0].Rows[x]["CartDate"].ToString()).ToString("yyyyMMdd");
//code to populate text file

      info = new UTF8Encoding(true).GetBytes(s);
      fs.Write(info, 0, info.Length);      
                        
}

info = new UTF8Encoding(true).GetBytes(h);
fs.Write(info, 0, info.Length);      

dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();      
EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
System.IO.File.Copy(LogFile, @"C:\temp\dd.txt");
Try something like this, and see if it helps
using System.IO;
 
...
 
        private string BuildLogFile(SqlTransaction trans, string flatFileLocation)
        {
            string logFile = flatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt";
 
            // Get the data from the stored procedure, and build a list of 
            // Cart dates in year-month-date format.  Write that string to
            // the log file location.
 
            using (DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS))
            {
                StringBuilder fileBuilder = new StringBuilder();
 
                foreach (DataRow dr in dsFlat.Tables[0].Rows)
                    fileBuilder.Append(DateTime.Parse(dr["CartDate"].ToString()).ToString("yyyyMMdd") + "\r\n");
 
                File.WriteAllText(logFile, fileBuilder.ToString());
 
                EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
 
                File.Copy(LogFile, @"C:\temp\dd.txt");
            }
            return logFile;
        }

Open in new window

Avatar of lobos

ASKER

I tried using some of your code....but I get this error as well
'System.IO.File' does not contain a definition for 'WriteAllText'

ALSO...
In my code sample I ommitted some of the details (//code to populate text file)....but I'm not sure how i can apply it using the code you posted...


LogFile = FlatFileLocation + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".txt"; 
 
using (FileStream fs = File.Create(LogFile)) 
{
DataSet dsFlat = SqlHelper.ExecuteDataset(trans, CommandType.StoredProcedure, spGET_NEW_TXNS);
 
for ( int x=0; x<dsFlat.Tables[0].Rows.Count;x++)
{    			
	string str = DateTime.Parse(dsFlat.Tables[0].Rows[x]["CartDate"].ToString()).ToString("yyyyMMdd"); 
 
	string s = "\r\n" + "\"" 
		+ dsFlat.Tables[0].Rows[x]["ROLL"].ToString() + "\"" 
		+ ", " + "\""
		+ this.GenerateCertificate(dsFlat.Tables[0].Rows[x]["Certificate"].ToString()) 
		+ "WEB"	+ "\"" 
		+", "+ "\"" 
		+ this.GetCertificateBalance(dsFlat.Tables[0].Rows[x]["TransactionID"].ToString()) 
		+  str + "\"" ;
 
	info = new UTF8Encoding(true).GetBytes(s);
	fs.Write(info, 0, info.Length);	
				
}
string h = "\r\n" + "\"" 
	+ "0000000000000000000" + "\"" 
	+ ", " + "\""
	+ "0000000"
	+ "WEB"	+ "\"" 
	+", "+ "\"" 
	+  "000000" + "\"" ;
 
info = new UTF8Encoding(true).GetBytes(h);
fs.Write(info, 0, info.Length);	
 
 
dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();	
EventLog.WriteEntry(@LogFile + @"C:\temp\dd.txt");
System.IO.File.Copy(LogFile, @"C:\temp\dd.txt");	

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial