finding an unknown string in a file

Joined
Jun 3, 2015
Messages
6
Programming Experience
Beginner
I have a very basic background with programming and I'm trying to learn more with C# (for Unity and projects related to Game development)... but I have a question that I'm hoping is fairly simple to answer. I have a file in a .sqf format (ArmAIII script). Inside this file are include statements pointing to other .sqf files.

I'm looking for a way to find the full path of each of these .sqf files contained within an open file, but the only thing I know about the path is that it will contain the file extension ".sqf". Can anyone provide some guidance on how to search for a full path (of unknown folder names)? So far I was able to find the index of where the string is... I'm also not sure if this algorithm makes sense, but I think I would have to read to the left of this index and find the first open quote, then read to the right and find the first close quote, then looping between these indices should build the correct path name. So far I can only get the indexOf string.

C#:
public static void CrawlFile(string file){
    string readText = File.ReadAllText(file);
    if (readText.Contains(".sqf")){
        Console.WriteLine(readText.IndexOf(".sqf"));
    }
}
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,183
Location
Norway
Programming Experience
10+
Regular expressions (Regex class) is often useful for finding strings by patterns. If the path is double-quoted such a pattern could be "*.sqf" (pseudo) if you understand what I'm getting at, actual regex by C# code would be similar.
 
Joined
Jun 3, 2015
Messages
6
Programming Experience
Beginner
I ended up using this pattern:

C#:
@"""\\rootDir\\(?:[^\\:]+\\)*((?:[^:\\]+)\.\w+)"""

This works for finding a path which begins with "\rootDir\ (which is how all the scripts in this project would reference files)...

I absolutely hate writing regex because I have a hard time understanding it, so I modified a sample that was found on another message board. I'm still not entirely sure what this does, but it is working.

What's important to note was that I ended up having to use StreamReader.ReadLine instead of File.ReadAllText (something I sampled from another script). The former method works because it searches the pattern on each line (4 occurrences in my sample)... The latter method read only 2 occurrences where the first contained 3 instance written one line after another and the second occurrence was on its own further down the page.
 

zerocool

Member
Joined
Aug 30, 2015
Messages
5
Programming Experience
3-5
As far as the regex goes, I'd use something like the following. You'd have to customize it to work with your read file but that shouldn't really be hard. I know you don't like regular expressions but honestly if you take a few hours to learn them, they're really not to bad and super useful in string manipulation and searching strings for patterns.

public static void Main(string[] args)
{


string[] filenames =
{
"matchfile1.sqf",
"matchedfile2.sqf",
"nomatch.sql",
"nomatch2.doc"
};

string pattern = ".sqf";


foreach (string s in filenames)
{
System.Console.Write(s);

if (System.Text.RegularExpressions.Regex.IsMatch(s, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
{
System.Console.WriteLine(" (match for '{0}' found)", pattern);
}
else
{
System.Console.WriteLine(" No Match Found");
}
}


}
 
Top Bottom