tags:

views:

40

answers:

3

Hi all i have done a code to save my file as follows

       if (m_strStandardEntryClassCode == "PPD")
        {
            m_strPath += "/PPD_BatchHeader_" + m_strDate + ".txt";
        }
        else
        {
            m_strPath += "/CCD_BatchHeader_" + m_strDate + ".txt";
        }
        using (TextWriter tw = new StreamWriter(m_strPath))
        {
            tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
            tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
            tw.Write(m_strCompanyName.PadRight(16, ' '));
            tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
            tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
            tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
            tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
            tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
            string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
            tw.Write(m_strEffDate.PadLeft(6, '0'));
            tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
            tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
            tw.Write(m_strBatchNumber.PadLeft(7, '0'));
            tw.Flush();
            tw.Close();
        }

Now i would like to save those two files in to single file and also as a multiple one. Can any one tell how to do this...

A: 

Various ways you can do it, I'd suggest.

  1. Copy file number 1 to a file called what the single combined file should be called.
  2. Open the new file (the single combined file) for appending.
  3. Open file number 2 for reading and read in all the contents.
  4. Write the data you've just read in to the single combined file.

This link has a simple code sample for doing something similar.

Another option would be to store all the data in memory and then write the 3 files straight from memory.

ho1
At the time of clicking on save is it possible to save
Dorababu
A: 

You'd want something like this.

Your data looks the same each time (or at least I can't see how the data differs) but I'm sure that you'll get the gist.

You need to use File.Append to add new data to a file and File.Open to clear and write new data. Call MergedDataWrite twice. If you need to write several files to it.

public void SeparateDataWrite()
{
  if (m_strStandardEntryClassCode == "PPD")
  {
    m_strPath += "/PPD_BatchHeader_" + m_strDate + ".txt";
  }
  else
  {
    m_strPath += "/CCD_BatchHeader_" + m_strDate + ".txt";
  }

  using (StreamWriter w = File.Open(m_strPath, FileMode.Create)
  {
    WriteData(w);
    w.close();
  }
}

public MergedDataWrite()
{
  using (StreamWriter w = File.Append("somefilename.txt")
  {
    WriteData(w);
    w.Close();
  }
}

public void WriteData(TextWriter tw)
{
            tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
            tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
            tw.Write(m_strCompanyName.PadRight(16, ' '));
            tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
            tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
            tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
            tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
            tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
            string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
            tw.Write(m_strEffDate.PadLeft(6, '0'));
            tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
            tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
            tw.Write(m_strBatchNumber.PadLeft(7, '0'));
            tw.Flush();
}
ChrisBD
A: 

Hi my actual code is like this i am little bit confused where to merge

  public bool saveBatchHeader(string m_strPath)
    {
        bool m_flag = true;
        string Filename = m_strPath;
        string m_strDate = DateTime.Now.ToString("MM/dd/yyyy");
        m_strDate = m_strDate.Replace("/", "");
        if (m_strStandardEntryClassCode == "PPD")
        {
            m_strPath += "/PPD_BatchHeader_" + m_strDate + ".txt";
            if (File.Exists(m_strPath))
            {
                int index = 1;
                Filename += "/PPD_BatchHeader_" + index + "_" + m_strDate + ".txt";
                while (File.Exists(Filename))
                {
                    string strFilePath;
                    strFilePath = Directory.GetCurrentDirectory();
                    strFilePath = Directory.GetParent(strFilePath).ToString();
                    strFilePath = Directory.GetParent(strFilePath).ToString();
                    strFilePath = strFilePath + "\\ACH\\";
                    Filename = strFilePath + "/PPD_BatchHeader_" + ++index + "_" + m_strDate + ".txt";
                }
                using (TextWriter tw = new StreamWriter(Filename))
                {
                    tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
                    tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
                    tw.Write(m_strCompanyName.PadRight(16, ' '));
                    tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
                    tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
                    tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
                    tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
                    tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
                    string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
                    tw.Write(m_strEffDate.PadLeft(6, '0'));
                    tw.Write(m_strJulianDate.PadRight(3, ' '));
                    tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
                    tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
                    tw.Write(m_strBatchNumber.PadLeft(7, '0'));
                    tw.Flush();
                    tw.Close();
                }
            }
        }
        else
        {
            m_strPath += "/CCD_BatchHeader_" + m_strDate + ".txt";
            if (File.Exists(m_strPath))
            {
                int index = 1;
                Filename += "/CCD_BatchHeader_" + index + "_" + m_strDate + ".txt";
                while (File.Exists(Filename))
                {
                    string strFilePath;
                    strFilePath = Directory.GetCurrentDirectory();
                    strFilePath = Directory.GetParent(strFilePath).ToString();
                    strFilePath = Directory.GetParent(strFilePath).ToString();
                    strFilePath = strFilePath + "\\ACH\\";
                    Filename = strFilePath + "/CCD_BatchHeader_" + ++index + "_" + m_strDate + ".txt";
                }
                using (TextWriter tw = new StreamWriter(Filename))
                {
                    tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
                    tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
                    tw.Write(m_strCompanyName.PadRight(16, ' '));
                    tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
                    tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
                    tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
                    tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
                    tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
                    string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
                    tw.Write(m_strEffDate.PadLeft(6, '0'));
                    tw.Write(m_strJulianDate.PadRight(3, ' '));
                    tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
                    tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
                    tw.Write(m_strBatchNumber.PadLeft(7, '0'));
                    tw.Flush();
                    tw.Close();
                }
            }

        }
        if (!(File.Exists(m_strPath)))
        {
            using (TextWriter tw = new StreamWriter(m_strPath))
            {
                tw.Write(m_strRecordTypeCode.PadLeft(1, '0'));
                tw.Write(m_strServiceClassCode.PadLeft(3, '0'));
                tw.Write(m_strCompanyName.PadRight(16, ' '));
                tw.Write(m_strCompanyDiscretionaryData.PadRight(20, ' '));
                tw.Write(m_strCompanyIdentification.PadRight(10, ' '));
                tw.Write(m_strStandardEntryClassCode.PadRight(3, ' '));
                tw.Write(m_strCompanyEntryDescription.PadRight(10, ' '));
                tw.Write(m_strCompanyDescriptiveDate.PadLeft(6, '0'));
                string m_strEffDate = m_strEffectiveEntryDate.Replace("/", "");
                tw.Write(m_strEffDate.PadLeft(6, '0'));
                tw.Write(m_strJulianDate.PadRight(3, ' '));
                tw.Write(m_strOriginatorStatusCode.PadRight(1, ' '));
                tw.Write(m_strOriginationDFIIdentification.PadLeft(8, '0'));
                tw.Write(m_strBatchNumber.PadLeft(7, '0'));
                tw.Flush();
                tw.Close();
            }
        }
        return m_flag;
    }

So how to merge those files

Dorababu
Can any one please help me regarding this.I am going to save different file names according to the selected combo box value. I will have my 2 values as CCD and PPD in my combo box. If the user selects CCD i will save it with a name as "CCD_BatchHeader_" + m_strDate + ".txt"; and if that name exists i will have an index for that and save same for PPD. Now at the time of saving i would like to save as individual file name as specified and also i would like to save them as Combined file by checking if any one of them exists
Dorababu