I have a C# application that, depending on certain database conditions, creates a new file, opens it, and writes a record from an input file into the file.
Each field in the record is separated with an "|", and certain conditions require a new file to be opened, an input file read, and copied, and an output file created that removes several of the records (Name, ID, and TINType) in the output file and keeps the "|" that separates the record. Several of the records can be different lengths, so I don't know the position of either of the substrings. I'm using IndexOf and RemoveSubstring to determine the places that need to be removed, but it seems to repeat the entire line once I get to Name, the first place that needs to be removed. Any ideas?
Here is the code:
switch (cCond)
{
case "1":
if (Regex.IsMatch(line, "\\|" + dUCode + "\\|", RegexOptions.IgnoreCase) && Regex.IsMatch(line, "\\|" + tType + "\\|", RegexOptions.IgnoreCase))
{
int pos = line.IndexOf("|");
int pos2 = line.IndexOf("|", pos + 1);
string DocUnitCode = line.Substring(pos + 1, pos2 - (pos + 1));
pos = 0;
pos2 = 0;
pos = line.IndexOf("|"); //start of file
lineA = lineA + line.Substring(0, pos); //add payment request code
pos = line.IndexOf("|"); //start of file
lineA = lineA + line.Substring(0, pos); //add department code
//pos = line.IndexOf("|", pos + 1); //end of department code
pos2 = line.IndexOf("|", pos + 1); //end of doc code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //doc code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of file unit code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //file unit code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of payment reference number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //payment reference number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of bank code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //bank code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of payment code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //payment code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of confirmation Code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //confirmation Code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Transaction Type
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Transaction Type
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Payment Amount
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Payment Amount
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of EFT Tracking Number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //EFT Tracking Number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Name
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Name
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //Remove name
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of ID Number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //ID Number
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //ID Number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of TIN Type
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //TIN Type
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //Remove TIN Type
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Return Reason Code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Return Reason Code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Return Reason Description
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Return Reason Description
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Addenda Information
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Addenda Information
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of EFT Settlement/Cleared date
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)) + "|"; //EFT Settlement/Cleared date
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Offset flag
if (pos2 < 0)
{
pos2 = Convert.ToInt32(line.Length - 1);
lineA = lineA + line.Substring(pos + 1, pos2 - pos) + "|";
}
else
{
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)) + "|"; //Offset flag
}
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Offset flag
if (pos2 < 0)
{
pos2 = Convert.ToInt32(line.Length - 1);
lineA = lineA + line.Substring(pos + 1, pos2 - pos);
}
else
{
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1));
}
string fName = strOutputFileDir + oFile + ".txt";
writeToFile(fName, line);
breakInnerLoop = true;
}
break;
Each field in the record is separated with an "|", and certain conditions require a new file to be opened, an input file read, and copied, and an output file created that removes several of the records (Name, ID, and TINType) in the output file and keeps the "|" that separates the record. Several of the records can be different lengths, so I don't know the position of either of the substrings. I'm using IndexOf and RemoveSubstring to determine the places that need to be removed, but it seems to repeat the entire line once I get to Name, the first place that needs to be removed. Any ideas?
Here is the code:
switch (cCond)
{
case "1":
if (Regex.IsMatch(line, "\\|" + dUCode + "\\|", RegexOptions.IgnoreCase) && Regex.IsMatch(line, "\\|" + tType + "\\|", RegexOptions.IgnoreCase))
{
int pos = line.IndexOf("|");
int pos2 = line.IndexOf("|", pos + 1);
string DocUnitCode = line.Substring(pos + 1, pos2 - (pos + 1));
pos = 0;
pos2 = 0;
pos = line.IndexOf("|"); //start of file
lineA = lineA + line.Substring(0, pos); //add payment request code
pos = line.IndexOf("|"); //start of file
lineA = lineA + line.Substring(0, pos); //add department code
//pos = line.IndexOf("|", pos + 1); //end of department code
pos2 = line.IndexOf("|", pos + 1); //end of doc code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //doc code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of file unit code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //file unit code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of payment reference number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //payment reference number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of bank code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //bank code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of payment code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //payment code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of confirmation Code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //confirmation Code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Transaction Type
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Transaction Type
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Payment Amount
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Payment Amount
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of EFT Tracking Number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //EFT Tracking Number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Name
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Name
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //Remove name
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of ID Number
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //ID Number
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //ID Number
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of TIN Type
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //TIN Type
lineA = lineA + line.Remove(pos + 1, pos2 - (pos + 1)); //Remove TIN Type
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Return Reason Code
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Return Reason Code
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Return Reason Description
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Return Reason Description
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Addenda Information
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)); //Addenda Information
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of EFT Settlement/Cleared date
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)) + "|"; //EFT Settlement/Cleared date
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Offset flag
if (pos2 < 0)
{
pos2 = Convert.ToInt32(line.Length - 1);
lineA = lineA + line.Substring(pos + 1, pos2 - pos) + "|";
}
else
{
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1)) + "|"; //Offset flag
}
pos = pos2;
pos2 = line.IndexOf("|", pos + 1); //end of Offset flag
if (pos2 < 0)
{
pos2 = Convert.ToInt32(line.Length - 1);
lineA = lineA + line.Substring(pos + 1, pos2 - pos);
}
else
{
lineA = lineA + line.Substring(pos + 1, pos2 - (pos + 1));
}
string fName = strOutputFileDir + oFile + ".txt";
writeToFile(fName, line);
breakInnerLoop = true;
}
break;
Last edited: