简体   繁体   中英

finding a string in a text file contained within a List<string>

I am trying to find out if a text file contains a string . The string could be any of the string 's that are contained within the list textwords , if a word is contained within the text file then it will copy the file it to a new location

The issue I am having is that the program does not loop through all the words in textwords it only takes the first value of the list. How do I get it to loop through all the strings within the list and see if they are contained within the text file before copying the file.

Any help would be appreciated.

My code is below,

foreach (FileInfo file in files)
{
    //reads the file contents
    bool nextouterloop = false;
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        //checks if the textwords are present in the file


        foreach ( string Keyword in textwords )
        {

            //if they are file is moved to quarantine messages

            if ( MessageContents.Contains(Keyword) )
            {
                try
                {
                    File.Copy(file.FullName,
                        @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages\" +
                        file);

                }
                catch ( IOException cannot_Move_File )
                {
                    MessageBox.Show(cannot_Move_File.ToString());
                }
                break;
            }
            //else it is moved To valid messages
            else
            {
                try
                {
                    File.Copy(file.FullName,
                        @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\" +
                        file);
                }
                catch ( IOException cannot_Move_File )
                {
                    MessageBox.Show(cannot_Move_File.ToString());
                }
                break;
            }
        }
    }

}

You have 'break' statements in both the 'if' and 'else' conditions. Therefore it will never loop beyond the first string.

But you are performing the copy and break in the first pass
No kidding it does not get to the second word

foreach (FileInfo file in files)
{    
    string path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\";
    //reads the file contents
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        //checks if the textwords are present in the file               
        foreach (string Keyword in textwords)
        {
            if (MessageContents.Contains(Keyword))
            {
                path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages\"
                break;
            }
        }                 
    }
    // let the StreamReader close down before the copy  
    // don't need it anymore  
    try
    {
        File.Copy(file.FullName, path + file);
    }
    catch (IOException cannot_Move_File)
    {
        MessageBox.Show(cannot_Move_File.ToString());
    }           
}

You are using break at the end of the statements. This will break the loop. You should use continue instead.

But this continue will be useless since you're using just an if-else function to do copy the file and nothing more. You can get rid of the break in this case. The program would execute the if statement and at the end of the block it will ignore the else statement (or vice-versa) and iterate the loop.

You have "break" causing it to stop after first iteration. Also a search like this wouldn't work. A better (yet not perfect) one is:

foreach (FileInfo file in files)
{
    //reads the file contents
    var content = File.ReadAllText(file.FullName);

    if (textwords.Any(tw => Regex.IsMatch(content, @"\b" + tw.Trim() + @"\b", RegexOptions.IgnoreCase))
    {
        try
        {
            File.Move(file.FullName, Path.Combine(@"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages", file.Name));
        }
        catch (IOException cannot_Move_File)
        {
            MessageBox.Show(cannot_Move_File.ToString());
        }
    }
    else
    {
        //else it is moved To valid messages
        try
        {
            File.Copy(file.FullName, @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\" + file.Name);
        }
        catch (IOException cannot_Move_File)
        {
            MessageBox.Show(cannot_Move_File.ToString());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM