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?
>>>>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?
ASKER
Thanks but I am getting error message...
System.IO.File.Copy(@LogFi le, @"C:\Temp\dd.txt");
The process cannot access the file "C:\Temp\dd.txt" because it is being used by another process.
System.IO.File.Copy(@LogFi
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
Bob
ASKER
LogFile = FlatFileLocation + DateTime.Now.ToString("yyy yMMdd_HHmm ss") + ".txt";
using (FileStream fs = File.Create(LogFile))
{
DataSet dsFlat = SqlHelper.ExecuteDataset(t rans, CommandType.StoredProcedur e, spGET_NEW_TXNS);
for ( int x=0; x<dsFlat.Tables[0].Rows.Co unt;x++)
{
string str = DateTime.Parse(dsFlat.Tabl es[0].Rows [x]["CartD ate"].ToSt ring()).To String("yy yyMMdd");
//code to populate text file
info = new UTF8Encoding(true).GetByte s(s);
fs.Write(info, 0, info.Length);
}
info = new UTF8Encoding(true).GetByte s(h);
fs.Write(info, 0, info.Length);
dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();
EventLog.WriteEntry(@LogFi le + @"C:\temp\dd.txt");
System.IO.File.Copy(LogFil e, @"C:\temp\dd.txt");
using (FileStream fs = File.Create(LogFile))
{
DataSet dsFlat = SqlHelper.ExecuteDataset(t
for ( int x=0; x<dsFlat.Tables[0].Rows.Co
{
string str = DateTime.Parse(dsFlat.Tabl
//code to populate text file
info = new UTF8Encoding(true).GetByte
fs.Write(info, 0, info.Length);
}
info = new UTF8Encoding(true).GetByte
fs.Write(info, 0, info.Length);
dsFlat.Clear();
dsFlat.Dispose();
fs.Flush();
EventLog.WriteEntry(@LogFi
System.IO.File.Copy(LogFil
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;
}
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...
'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");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
System.IO.File.Copy(@"C:\T
Bob