HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpMinor

I have an encrypted file, how to remove a specific string from that file

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fileencryptedremovethathowfromstringspecifichave

Problem

I got a file with lots of ASCII characters. No extension, and has the Type Of file attribute 190000 File.

I have many characters like L¿ö in the file however I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

I tried the following code. And got everything working. Indexes are 3 and 19001 respectively,

VB:

Dim Load_File = System.IO.File.ReadAllText("C:\Documents and Settings\Fousu.s\Desktop\Files\SYSI091512.190000")
        Dim EditedString = ""
        Dim IndexofPYGR = Load_File.IndexOf("PYGR")
        Dim indexOfMCG = Load_File.LastIndexOf("MCG")
        Dim LengthMCG = "MCG"

        'System.Console.ReadKey()
        If ((IndexofPYGR > -1) And (indexOfMCG > -1)) Then
            EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1)))
            System.IO.File.WriteAllText("C:\Documents and Settings\Fousu.s\Desktop\Files\SYSI091512.190000V1", EditedString)
        End If


c#

//Getting the file as a string.
            string Load_File = System.IO.File.ReadAllText(@"C:\Documents and Settings\Fousu.s\Desktop\Files\SYSI091512.190000");

            string EditedString;
            int IndexofPYGR = Load_File.IndexOf("PYGR");
            int indexOfMCG  = Load_File.IndexOf("MCG");
            string LengthMCG = "MCG";
            //Console.WriteLine("Index of PYGR is : {0}", IndexofPYGR);
            //Console.WriteLine("Index of MCG is : {0}", indexOfMCG);
            //System.Console.ReadKey();   
            if ((IndexofPYGR != -1) && (indexOfMCG != -1))
            {
                EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1))); 

                System.IO.File.WriteAllText(@"C:\Documents and Settings\Fousu.s\Desktop\Files\SYSI091512.190000V1", EditedString);


Would like to request a review of the above code, or is there a better way to achieve this? and also would like to know how to get the Type Of File attribute to 190000.

Solution

Some clarifications

I got a file with lots of ASCII characters. [...] I have many characters like L¿ö in the file [...]

As others have pointed out, these are not ASCII characters.

No extension, and has the Type Of file attribute 190000 File.

Configure Windows Explorer to show file extensions.

I required to remove lines from a word PYGR to another word MCG. It has several lines in between.

Your code tells us otherwise. You are not removing the lines from PYGR to MCG; your code removes the word PYGR and every character that follows up to one character before MCG. I assume that is not quite what you were trying to achieve?

If you update your question to explain if you are trying to remove everything between (excluding) PYGR and MCG, including one of them or including both of them, I'll gladly adapt my answer. For now, I will assume the latter.

Edit: All right, in accordance with your comment I have changed my answer to replace all text from PYGR (inclusive) to MCG (exclusive).
Reviewing your code
Readability

  • Please use the well-established C# naming conventions: local variables are camelCase



  • Format your code with the correct indentation to make it more readable



  • Start your file with using System.IO to avoid repeating yourself



  • Get rid of unnecessary parenthesis



  • Choose proper names



  • Join declaration and usage of variables where possible



For example:

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1)));


should be more like

string editedContent = fileContent.Remove(startIndex, endIndex - end.Length);


Maintainability

Hard-coding the paths is something that should be avoided even during development. You can use Environment.GetFolderPath with Environment.SpecialFolder.Desktop to retrieve the path to the current user's desktop and add the file name with Path.Combine.
Regex

I really don't like the heave use of indexOf: your code seems to be all about how the replacement is done, instead of what is going on (i.e. it is imperative, not declarative).

Using a Regex, we can arrive at a more declarative style. You may need to adjust this to suit your needs (your question is ambiguous).

Refactored

string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1");   

// you would probably pass the paths into your method as parameters

var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

Code Snippets

string EditedString;
// ...
EditedString = Load_File.Remove(IndexofPYGR, (indexOfMCG - (LengthMCG.Length + 1)));
string editedContent = fileContent.Remove(startIndex, endIndex - end.Length);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string inFilePath = Path.Combine(desktop, "SYSI091512.190000");
string outFilePath = Path.Combine(desktop, "SYSI091512.190000V1");   

// you would probably pass the paths into your method as parameters

var fileContent = File.ReadAllText(inFilePath);
var result = Regex.Replace(fileContent, "PYGR.*MCG", "MCG", RegexOptions.Singleline);
File.WriteAllText(outFilePath, result);

Context

StackExchange Code Review Q#15027, answer score: 8

Revisions (0)

No revisions yet.