Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

C# Open Text File, Read First Line for a Specific Word, If the Word Exists, Move the File

0.00/5 (No votes)
10 Mar 2014 2  
C# - Read Text File, Match a Word in First Line, Move File if Word Matches

Introduction

If a group of files reside in a folder (directory) and a file in the group needs to be moved to another folder (directory) if a specific word is in the first line of the text file, then the file containing the specific word is moved leaving the remainder of the files without the specific word in the folder.

Background

Because I write code in LISP as part of using AutoCAD, I reviewed LISP files in a folder to determine if the LISP file worked. After testing the LISP file in AutoCAD and if the LISP code failed to execute (run) properly, I added the words "Doesn't" work in the first line of the LISP file. Because there were a large number of LISP files that I reviewed, 1,103 files, I didn't want to open each file to find the words "Doesn't" work and then move the file, I wrote a C# program to read the first line of each file which has the file name and a description of the purpose of the LISP code and then wrote the first line of the file to a text file. I printed the file to determine which files contained the words "Doesn't" work. Because there were about 200 to 300 files that contained the words, "Doesn't" work, and I didn't want to manually go through the list and move each file to another folder and manually mark the list as the file being moved, I wrote a C# program to move the files for me, by opening each file and looking at the first line of each LISP text file for the word "Doesn't" and if the word is in the first line, the file is moved to another folder.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MF // Move File
{
    public partial class Form1 : Form // Requires a Form1 to execute (run)
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) // Requires a button on Form1 to execute (run)
        {
            string sourceDir = @"C:\VIDF\"; // FROM FOLDER (DIRECTORY)
            string destinDir = @"C:\VIDT\"; // TO FOLDER (DIRECTORY)

            string[] files = Directory.GetFiles(sourceDir); // GET FILE NAMES IN FROM FOLDER
            foreach (string file in files) // GO THROUGH THE FILES IN THE FOLDER FOR THEIR NAMES
            {
                using (StreamReader readIt = new StreamReader
                (sourceDir + Path.GetFileName(file))) // GET A TEXT FILE AND OPEN THE FILE
                {
                    string line = readIt.ReadLine(); // READ THE FIRST LINE OF THE TEXT FILE
                    if (line.Contains("DOESN'T")) // LOOK FOR THE WORD "DOESN'T"
                    {
                        File.Copy(file, destinDir + Path.GetFileName(file)); // IF THE WORD 
                        //"DOESN'T APPEAR COPY THE FILE TO THE DISTINATION FOLDER (DIRECTORY)
                        string holdName = sourceDir + Path.GetFileName(file); // HOLD THE COPIED FILE NAME
                        readIt.Close(); // CLOSE THE readIt.ReadLine();
                        File.Delete(holdName); // DELETE THE FILE
                        // NOTE FOR SOME UNKNOWN REASON THE File.MoveTo 
                        // WILL NOT WORK AND I CAN FIND NO REASON.
                        // ONLY THIS PROCEDURE SEEMS TO WORK
                    } // End if
                    readIt.DiscardBufferedData();  // KILL THE BUFFER
                    readIt.Close(); // CLOSE THE readIt.Readlin
                } // End Using StreamReader
            } // End foreach

            Environment.Exit(0); // CLOSE THE FORM AFTER EXECUTION OF THE CODE
        } // End foreach
    } // End public Form1
} // Namespace end

Points of Interest

In developing this code, I could not find a reason the File.Move did not work. The approach of copying the file and then deleting the first file was the only way to get the program to work.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here