C# help ....can't figure out whats going wrong

  • Here i am appending two files(f1, f2) into one file(final)

    F1, F2 are the variables external

    Everything seams fine .... but i get the final file loaded up with data of f2 file only ...Its not taking up f1 file

    ### i checked with commenting f2 file part, then final file loads up with f1 file...its only taking one file and loading up ###

    Can anyone look at this and tell me whats wrong ..PLz

    public void Main()

    {

    string fs1, fs2, fin, Messtype;

    fs1 = Dts.Variables["f1"].Value.ToString();

    fs2 = Dts.Variables["f2"].Value.ToString();

    fin = Dts.Variables["final"].Value.ToString();

    FileInfo fso1 = new FileInfo(fs1);

    FileInfo fso2 = new FileInfo(fs2);

    FileInfo fino = new FileInfo(fin);

    if (!fso1.Exists)

    {

    Messtype = " doesn't exsist" + fs1;

    MessageBox.Show(Messtype);

    Dts.TaskResult = (int)ScriptResults.Failure;

    }

    StreamWriter writer;

    StreamReader reader;

    string rs;

    writer = fino.CreateText();

    reader = File.OpenText(fs1);

    rs = null;

    while ((rs = reader.ReadLine()) != null)

    {

    writer.WriteLine(rs);

    Console.Write("fs1 completed");

    }

    writer.Close();

    reader.Close();

    StreamWriter wr;

    StreamReader rd;

    string rsw;

    rsw = null;

    wr= fino.CreateText();

    rd = File.OpenText(fs2);

    while ((rsw = rd.ReadLine()) != null)

    {

    wr.WriteLine(rsw);

    Console.Write("fs2 completed");

    }

    wr.Close();

    rd.Close();

    Dts.TaskResult = (int)ScriptResults.Success;

    }

  • I think that your problem is that when you open the writer the second time it overwrites the contents of the file.

    To avoid this, just open the writer at the beginning and close it at the end - don't close it between operations.

    Or use .AppendText the second time, instead of CreateText.

    Or let the o/s do it all for you in one line (sorry, can't remember the copy syntax).

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Thanku for the reply ...but whats this o/s ???

  • he means the operating system...command prompt's COPY command does it in a single line: you could call the Shell command for it:

    in SQL

    EXEC Master.dbo.xp_CmdShell 'COPY C:\Temp\Header.txt+C:\Temp\Test01.txt C:\Temp\MyWork.txt'

    in a command window, i added the dbl quotes for long file names as a model

    COPY "C:\Temp\Header.txt" + "C:\Temp\Test01.txt" "C:\Temp\MyWork.txt"

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply