Home > Blockchain >  How to add a closing Parenthesis to the end of a line in C#?
How to add a closing Parenthesis to the end of a line in C#?

Time:11-22

I want to add a closing Parenthesis to each line in the text that ends with "CSQL_CREATE_VIEW (" some words ")"

So that the end result looks like this: CSQL_CREATE_VIEW (name of view) /n. I am not able to get the closing bracket with my current code. Help!

   string[] Files = System.IO.Directory.GetFiles(@"filepath");
            string path = @"outputTextPath";


            foreach (string s in Files)
            {
            
;
                string fileCont = System.IO.File.ReadAllText(s);
                if(fileCont.Contains("create view") == true)
                {
                    File.AppendAllText(path, fileCont.Replace("create view","CSQL_CREATE_VIEW (") );
                     if (fileCont == "\r" && fileCont.Contains("CSQL_CREATE_VIEW ("))
                    {
                        File.AppendAllText(path, ")"   Environment.NewLine);
                    }
                        

                }
              

            }

CodePudding user response:

this can never be true

if (fileCont == "\r" && fileCont.Contains("CSQL_CREATE_VIEW ("))

if fileCont == "\n" then it cannot contain "CSQL....."

maybe you mean fileCont.Endwith("\n")

CodePudding user response:

This approach sounds very error prone, since it's making a lot of assumptions about the contents of the file, but I'll leave it to you to determine if this is really the approach you want to take.

If I understand your problem description correctly, you want to replace the text "create view" with "CSQL_CREATE_VIEW (" and also add a closing parenthesis to the end of that line.

If that's all we need to do, one way to approach this is to read one line of the file at a time instead of reading all the text at once, and we can keep another list of "new" lines that we'll use to write the new file. As we read each line from the input file, we can save that line (with any necessary changes) to our new list. After we've read all the lines, we can save our new list of lines to the output file.

For example:

public static void ReplaceCreateView(string inputFilePath, string outputFilePath)
{
    List<string> newFileLines = new List<string>();

    foreach (string line in File.ReadLines(inputFilePath))
    {
        if (line.Contains("create view"))
        {
            // Save a line where we replace 'create view' and add a closing parenthesis
            newFileLines.Add(line.Replace("create view", "CSQL_CREATE_VIEW (")   ")");
        }
        else
        {
            newFileLines.Add(line);
        }
    }

    File.WriteAllLines(outputFilePath, newFileLines);
}

It looks like you're updating all files in a particular directory, so one way to do this would be to loop through all the files, create a new file name based on the original file name plus some other text, like "_Updated", and then pass the original file name and new file name to our method above in a loop:

public static void UpdateAllFiles(string directoryPath)
{
    foreach(var filePath in Directory.GetFiles(directoryPath))
    {
        // Create a new name for the file by appending '_Updated' to the orginal
        var fileName = Path.GetFileNameWithoutExtension(filePath);
        var ext = Path.GetExtension(filePath);
        var newFilePath = Path.Combine(directoryPath, $"{fileName}_Updated{ext}");

        ReplaceCreateView(filePath, newFilePath);
    }
}

CodePudding user response:

Not sure what your intention with the output file was. You're reading possible multiple input files and only outputting to one file?

At any rate, here is how you would change the contents of each individual file, keeping the same name:

String[] Files = System.IO.Directory.GetFiles(@"filepath");
foreach (String s in Files)
{
    Boolean changesMade = false;
    String[] lines = System.IO.File.ReadAllLines(s);
    for(int i=0; i<lines.Length; i  )
    {
        if (lines[i].Contains("create view"))
        {
            changesMade = true;
            lines[i] = lines[i].Replace("create view", "CSQL_CREATE_VIEW (");
            if (!lines[i].EndsWith(")"))
            {
                lines[i] = lines[i]   ")";
            }
        }
    }
    if (changesMade)
    {
        System.IO.File.WriteAllLines(s, lines);
    }
}

It would be helpful if you'd post a couple of before and after examples of a complete line in question...

CodePudding user response:

The best approach would be Regex in my opinion. I don't fully understand how your text looks like but this pattern should work: ((CSQL_CREATE_VIEW \()(.*)(\r|\n|\r\n))

Use a page like this to test your regex against your file content.

  •  Tags:  
  • c#
  • Related