-1

Can someone help me, I just learning C# for about 2 month, I have this problem, i'm building a class for filter data from temp file and create the result in new txt file inside directory, if directory is empty nor at the same date, it build perfectly, and if there another file at same date it should create with increase the last number at lastname.

My problem when I run code, it is not creating if the directory has files with the same dates, then the result should be something like this:

  • C:\result_2014051301.txt
  • C:\result_2014051401.txt
  • C:\result_2014051402.txt <-- Failed, it is not ..2014051401.txt

     class Entity2
     {
        public Entity2()
        {
           string fileTemp = "DEFAULT.temp";
           string indexD = Properties.Settings.Default.ChIndex2D;
           string indexC = Properties.Settings.Default.ChIndex2C;
           string indexS = Properties.Settings.Default.ChIndex2S;
           string tempPath = AppDomain.CurrentDomain.BaseDirectory;
           string targetPath = Properties.Settings.Default.ExtractALL_DIR;
           string SourceFile = Path.Combine(tempPath, fileTemp);
           string tempFileX = Path.GetTempFileName();
    
           if (!System.IO.Directory.Exists(targetPath))
           {
               System.Windows.Forms.MessageBox.Show("Error missing .temp", "Message Box");
           }
           else
           {
               string ext = ".txt";
               int sequence = 0;
               DateTime dateFileName = DateTime.Today;
               string discode = Properties.Settings.Default.ChannelID_2;
               string filename = discode + "_" + dateFileName.ToString("yyyyMMdd");
               string pathX = Properties.Settings.Default.ExtractALL_DIR + @"/Channel2";
    
               if (!Directory.Exists(pathX))
               {
                   Directory.CreateDirectory(pathX);
               }
    
               string[] files = Directory.GetFiles(pathX, filename + "*.txt", SearchOption.TopDirectoryOnly);
               if (files.Length > 0)
               {
                   Array.Sort(files);
                   string lastFilename = files[files.Length - 1];
                   sequence = Int32.Parse(lastFilename.Substring(0, lastFilename.Length - 4).Replace(pathX + filename, ""));
    
               }
    
               sequence++;
    
               string newFileName = filename + sequence.ToString().PadLeft(2, '0') + ext;
               string DestFile = Path.Combine(pathX, newFileName);
    
               using (var ab = new StreamReader(SourceFile))
               using (var cd = new StreamWriter(DestFile))
               {
                  string lineX;
    
                  while ((lineX = ab.ReadLine()) != null)
                  {
                    if (lineX.LastIndexOf("100", 3) != -1 || lineX.LastIndexOf("MGR", 15) != -1 || lineX.LastIndexOf(indexC, 15) != -1)
                    {
                        lineX = lineX.Replace(indexD, "");
                        lineX = lineX.Replace("DEFAULT", discode);
                        if (lineX.LastIndexOf("800", 3) != -1)
                        {
                            lineX = lineX.Replace(indexS, "");
                        }
                        cd.WriteLine(lineX);
                    }
    
                  }
               }
           }
        }
     }
    
Surya
  • 15,703
  • 3
  • 51
  • 74
  • Be sure to include any exceptions that occur when your application fails, they are very helpful in finding the root of the problem. – Guvante May 15 '14 at 18:24

1 Answers1

0

This piece is not functioning correctly:

Int32.Parse(lastFilename.Substring(0, lastFilename.Length - 4).Replace(pathX + filename, ""));

pathX + filename is C:\folderfile.txt not C:\folder\file.txt.

You either need to add the \ or call Path.Join.

That will cause the Parse operation to fail since it tries to consume the who string (minus the extension).

Guvante
  • 18,775
  • 1
  • 33
  • 64