✅ 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.
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
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!");
}
I want to run a foreach line function as there are more videos that the user has to find
Checkout File.ReadAllLines: https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalllines?view=net-7.0
File.ReadAllLines Method (System.IO)
Opens a text file, reads all lines of the file into a string array, and then closes the file.
umm this only reads all the lines. my issue is with string comparison.
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.
well the comparison still fails
I used this following code:
is there a regx method of comparing strings from the text file ?
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
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:
can I use line.replace( " 0", "") ?
Does the line always have a number at the beginning?
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
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.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.
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] + ")" );
}
}
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.
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:
thank you so much for your help as it did give me this idea to use this code 😄