Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 594
  • 7.4k

Delete a dynamic String value from path

Jun 6 2024 8:21 PM

Hello,

I need help on how to delete a dynamic string within a path using Regex: 

".\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_0001\" . The last number: _0001\"

".\LOANS_ADVERSEACTION_00000000_00000000_0001\" . The last number: _0002\"

".\MEMBERRECORDS_IRADOCUMENTS_00000000_00000000_0001\" . The last number: _0003\"

So far I have use many similar Regex to this: ^[^_]*_[^_]*   But definitely I am ot applying it correectly, it is not working.

Also in the 3rd column where the value is tif or pdf change it for a numeric value, per example: tif = 2, pdf = 16

143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613742_001.tif||713797998|
143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613749_001.tif||3633|||11/143|LOANSADVERSEACTION|PDF|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613751_001.tif||713799426|143|LOANSADVERSEACTION|tif|1|.\LOANS_ADVERSEACTION_00000000_00000000_0001\613755_001.tif||5229||07/0

This is my code:

		private void button2_Click(object sender, EventArgs e)
		{
			try
			{
				if (string.IsNullOrEmpty(FromFile))
				{
					lblStatus.Text = "Missing CSV Information";
					MessageBox.Show("Please select a CSV file first.");
					return;
				}

				if (string.IsNullOrEmpty(ToFile))
				{
					lblStatus.Text = "Missing Save Information";
					MessageBox.Show("Please enter save information.");
					return;
				}
				else if (File.Exists(ToFile))
				{
					File.Delete(ToFile);
				}

				btnProcess.Enabled = false;
				lblStatus.Text = "Processing...";
				//TIF = 2, PDF= 16 column 6
				var lines = File.ReadAllLines(FromFile).Skip(1);
				string docId = "";
				string[] oldLine = null;

				string pattern = @"\.\\MEMBERRECORDS_SIGNATURECARD_00000000_00000000_\d{4}\\|\.\\LOANS_ADVERSEACTION_00000000_00000000_\d{4}\\|\.\\MEMBERRECORDS_IRADOCUMENTS_00000000_00000000_\d{4}\\";
				//string pattern = @"\.\\^[^_]*_[^_]*_00000000_00000000_\d{4}\\";
				var sb = new StringBuilder();

				foreach (var line in lines)
				{
					var newLine = Regex.Replace(line, pattern, "").Replace("\"", "").Split(',');

					if (docId != newLine[0])
					{
						docId = newLine[0];
						oldLine = newLine;
					}
					else
					{
						for (int i = COPY_FROM - 1; i < newLine.Length; i++)
							newLine[i] = oldLine[i];
					}

					sb.AppendLine(string.Join("|", newLine));
				}

				File.WriteAllText(ToFile, sb.ToString());

				//btnProcess.Enabled = true;
				//lblStatus.Text = "Completed";
			}

Any help will be helpful and appreciate it. Thank you.


Answers (3)