C
C#2y ago
Surihia

✅ Getting a string from a text file and get the whole line containing the string

I am making a app where I want to get the filename of a video file and then check if the filename exists in a text file. if it exists, then I want to get the line containing that string and I have few problems with this. The video filename is "VIDEO_PRODUCT_AD01_AVI.mp4" This very filename is stored in the text file as this string: "video_product_AD01_avi.mp4" This filename is usually stored along with the file path in this way along with a lot of video filenames in this text file.
20797607 ..\Video\Products\video_product_AD01_avi.mp4
20797607 ..\Video\Products\video_product_AD01_avi.mp4
Now I know how to get the filename as a string from the video file but then matching that filename with the respective filename string in the text file, is where I am facing problems. I don't want to change the video filename or the filename string in the text file. is there any solution to this ?
15 Replies
Axiss
Axiss2y ago
If I'm understanding the problem right, wouldn't something like this work? var lineFromFile = @"..\Video\Products\video_product_AD01_avi.mp4"; var fileName = "VIDEO_PRODUCT_AD01_AVI.mp4"; if(lineFromFile.EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase)) { Console.WriteLine("Found it!"); }
Surihia
SurihiaOP2y ago
I want to run a foreach line function as there are more videos that the user has to find
Axiss
Axiss2y ago
File.ReadAllLines Method (System.IO)
Opens a text file, reads all lines of the file into a string array, and then closes the file.
Surihia
SurihiaOP2y ago
umm this only reads all the lines. my issue is with string comparison.
Axiss
Axiss2y ago
I'm not sure what you are looking for then. I gave you a way to get all the lines and a way to do the string comparison.
Surihia
SurihiaOP2y ago
well the comparison still fails I used this following code:
var fileName = "VIDEO_PRODUCT_AD01_AVI.mp4";

foreach(var line in File.ReadAllLines("textFile.txt"))
{
if (line.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("Found it!");
}
}

Console.Readline();
var fileName = "VIDEO_PRODUCT_AD01_AVI.mp4";

foreach(var line in File.ReadAllLines("textFile.txt"))
{
if (line.Equals(fileName, StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("Found it!");
}
}

Console.Readline();
is there a regx method of comparing strings from the text file ?
Axiss
Axiss2y ago
if you are doing .Equals they have to match exactly. I used .EndsWith because you want to ignore all the directory info at the beginning of the line
Surihia
SurihiaOP2y ago
I forgot to mention one more aspect when I mentioned the path names. Some of the paths has a 0 after the file extension. like this:
20797607 ..\Video\Products\video_product_AD01_avi.mp4 0
20797607 ..\Video\Products\video_product_AD01_avi.mp4 0
can I use line.replace( " 0", "") ?
Axiss
Axiss2y ago
Does the line always have a number at the beginning?
Surihia
SurihiaOP2y ago
yes. those numbers are what I need to change from the text file and for that I am using the filename to catch the line. so as it catches the line, I would edit the number with a different value. here it is the size value in bytes
Axiss
Axiss2y ago
ahh.. do line.Split(' ')[1].EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase) that splits the line on a space, creating an array of the parts of the string, and then gets the second item in the array.
Surihia
SurihiaOP2y ago
can you like provide a sample code with this running in a for each loop ? I did use the split method once and stored each split part in string arrays. but for this I am a bit lost on what to do. using the ' ' as a splitting parameter, I assume you are splitting the path into 3 different parts.
Axiss
Axiss2y ago
var linesFromFile = new [] { @"1234 ..\Video\Products\video_product_AD00_avi.mp4 0", @"5678 ..\Video\Products\video_product_AD01_avi.mp4 1", @"9012 ..\Video\Products\video_product_AD02_avi.mp4 2" }; var fileName = "VIDEO_PRODUCT_AD01_AVI.mp4"; foreach(var line in linesFromFile) { var lineParts = line.Split(' '); if(lineParts[1].EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase)) { Console.WriteLine("Found it: " + lineParts[1] + " (" + lineParts[0] + ")" ); } }
Axiss
Axiss2y ago
Here is a fiddle of it: https://dotnetfiddle.net/6IE1Uk
C# Online Compiler | .NET Fiddle
Test your C# code online with .NET Fiddle code editor.
Surihia
SurihiaOP2y ago
thanks. I will see what I can do now 🙂 Had to use a different code from one of my older programs to do the updating of the size value:
var fileName = "VIDEO_PRODUCT_AD01_AVI.XXX";
var totalLines = File.ReadLines("textFile.txt").Count();
StreamReader sr = new StreamReader("textFile.txt");

FileStream fs = new FileStream("textFile2.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);


var lineCounter = 1;


for (int i = 0; i < totalLines; i++)
{
string Parsed = sr.ReadLine();
string[] data = Parsed.Split(' ');
int Size = Convert.ToInt32(data[0]);
int Size_2 = Convert.ToInt32(data[1]);
string Path = Convert.ToString(data[2]);
int Size_3 = Convert.ToInt32(data[3]);

if (Path.EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("At line: " + lineCounter +
" Info: " + Size + " " + (int)Size_2 + " " +
Path + " " + (int)Size_3);
Size = 2000000; // custom test size value
}

string currentLine = (int)Size + " " + (int)Size_2 + " " + Path + " " + (int)Size_3;
sw.WriteLine(currentLine);
lineCounter++;
}

sr.Close();
sw.Close();
fs.Close();

Console.ReadLine();
var fileName = "VIDEO_PRODUCT_AD01_AVI.XXX";
var totalLines = File.ReadLines("textFile.txt").Count();
StreamReader sr = new StreamReader("textFile.txt");

FileStream fs = new FileStream("textFile2.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);


var lineCounter = 1;


for (int i = 0; i < totalLines; i++)
{
string Parsed = sr.ReadLine();
string[] data = Parsed.Split(' ');
int Size = Convert.ToInt32(data[0]);
int Size_2 = Convert.ToInt32(data[1]);
string Path = Convert.ToString(data[2]);
int Size_3 = Convert.ToInt32(data[3]);

if (Path.EndsWith(fileName, StringComparison.CurrentCultureIgnoreCase))
{
Console.WriteLine("At line: " + lineCounter +
" Info: " + Size + " " + (int)Size_2 + " " +
Path + " " + (int)Size_3);
Size = 2000000; // custom test size value
}

string currentLine = (int)Size + " " + (int)Size_2 + " " + Path + " " + (int)Size_3;
sw.WriteLine(currentLine);
lineCounter++;
}

sr.Close();
sw.Close();
fs.Close();

Console.ReadLine();
thank you so much for your help as it did give me this idea to use this code 😄
Want results from more Discord servers?
Add your server