Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for string inthe latest file. 1

Status
Not open for further replies.

Forum25

Programmer
Aug 8, 2007
5
US
1) how to search for the latest file in a folder? folder contains files named as mmddyy.txt
2) how to search for a string in the file?
 
hi dseaver

This is what have in VB so far:

dim fPath as string = "\\server\folder\mmddyy.txt"
Dim fStream As FileStream = New FileStream(fPath, FileMode.Open, FileAccess.Read, FileShare.Read)
dim fr as new streamReader(fStream)

while not fr.EndOfStream
dim lineText as string = fr.Read
if instr(lineText,"Copy not sent") <> 0 Then
'do something with it
end if
end while

But I don't know how to do it in C#. I do not know how to search for the latest mmddyy.txt in the folder. and the biggest do not know is how to access the server where the files reside. the server is on the same network and it has a username and password to connect to

thank you much
 
You could use Directory.GetFiles(folder) to store the paths of all the files in that folder, sort the string[] by name, if you don't know the date of the latest file, or search the array for the date in question. Once you have the file, use a text reader to readline() the file and do line.contains to find the string.
Code:
string fileInQuestion;
string[] fileNames;
fileNames = Directory.GetFiles(@"C:\folder");

Sort(fileNames);
fileInQuestion = fileNames[fileNames.Length - 1];
//You will need to write the sort function


//if you know the date
string dateNeeded;
foreach(string filename in fileNames)
{
    if(filename.Contains(dateNeeded))
          fileInQuestion = filename;
}

StreamReader fileReader = new StreamReader(fileInQuestion);
string line, findMe;
while(fileReader.Peek() > 0)
{
    line = fileReader.Readline();
    if(line.Contains(findMe))
         //Do Something
}

The Sort Function wouldn't be hard to write.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top