0

So I am writing a C# program which combines several text files into one and saves them as a combined text file. One issue I am having, I have a textfield which selects the intended folder the save the compiled reciept, however when selecting the desired folder, it generates a file name to the text box, the filename follwing the final / must be erased every time for the save function to work properly. I am wondering, how to remove all text after the final letter before the last / in the file directory?

Here is the code:

private void RecieptDisplayed_TextChanged(object sender, EventArgs e)
{
    try
    {
        string[] fileAry = Directory.GetFiles(RecieptSelect.Text);

        string input = RecieptSelect.Text;
        int index = input.LastIndexOf("/");
        if (index >= 0)
            input = input.Substring(0, index);

        MessageBox.Show("Reciepts being processed : " + index);

        using (TextWriter tw = new StreamWriter(savefileas.Text + "RecieptsCombined.txt", true))
        {
            foreach (string filePath in fileAry)
            {
                using (TextReader tr = new StreamReader(filePath))
                {
                    tw.WriteLine("Reciept for: " + " " + filePath + tr.ReadToEnd()) ;
                    tr.Close();
                    tr.Dispose();
                }
                MessageBox.Show("File Processed : " + filePath);
            }

            tw.Close();
            tw.Dispose();
        }
    }
jazb
  • 5,498
  • 6
  • 37
  • 44

3 Answers3

1

You have a string like

var fullpath = @"C:\temp\myfile.txt";

You can use:

var dir = Path.GetDirectoryName(fullpath);

To get

c:\temp

Note that if the path ends with a slash it doesn't remove it before "going up a directory" so c:\temp\ becomes c:\temp. Try to keep your paths free of trailing slashes

Try to always use the Path class when manipulating string that are paths. It has a whole load of useful methods (this isn't an exhaustive list but the ones I use most) like:

GetFileName
GetFileNameWithoutExtension
GetExtension 
ChangeExtension
Combine

This last one builds paths, eg:

Path.Combine("c:", "temp", "myfile.txt");

It knows the different operating systems it runs on and builds paths appropriately - if you're using net core on Linux it uses "/" instead of "\" for example. full docs here

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
0

You are looking for Directory name from given path, you can use existing function to get the directory name, Path.GetDirectoryName()

using System.IO;
...

//Get the directory name
var directoryName = Path.GetDirectoryName(savefileas.Text);

using (TextWriter tw = new StreamWriter(Path.Combine(directoryName, "RecieptsCombined.txt"), true))

{
      foreach (string filePath in fileAry)
      {
            using (TextReader tr = new StreamReader(filePath))
            {
                tw.WriteLine("Reciept for: " + " " + filePath + tr.ReadToEnd()) ;
                tr.Close();
                tr.Dispose();
            }
            MessageBox.Show("File Processed : " + filePath);
      }

      tw.Close();
      tw.Dispose();
}
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

Construct a FileInfo object from the string and then use DirectoryName or Directory.

Also, do not concatenate strings to get a file name, use Path.Combine instead.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222