Click here to Skip to main content
Click here to Skip to main content

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

, 10 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
C# - Read Text File, Match a Word in First Line, Move File if Word Matches


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.


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()

        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 
                        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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Engineer - Civil Engineering Design, Inc. (Oregon)
United States United States
AS - General Studies
AS - Computer Science
Cert. - Architectural Drafting
AAS - Web and Database Technology
Cert. - Database Technology
BS - General Studies
BS - Science
BS - Psychology
BS - Social Science
BS - Civil Engineering
MS - Education: Information Technology
PH.D. - Incomplete. Design & Human Environment [Housing]
Civil Engineer (OR, WA, CA)
AutoCAD Expert
AutoCAD LISP Programmer
Visual Studio (C#.NET & VB.NET)
Microsoft Excel (VBA Programmer)

Comments and Discussions

GeneralMy vote of 1 PinmemberRev1.011-Mar-14 22:15 
Suggestionyou cann't move the file before you close it PinmemberCosmic_Spy11-Mar-14 5:58 
GeneralMy vote of 2 PinmemberMember 105112177-Mar-14 2:28 
QuestionSpecific, and rough PinprotectorChristian Graus6-Mar-14 18:04 
QuestionUsing is not a good way! Pinmemberdroka19846-Mar-14 17:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 10 Mar 2014
Article Copyright 2014 by caed
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid