Click here to Skip to main content
15,885,988 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.38/5 (5 votes)
10 Mar 2014CPOL2 min read 51.5K   9   5
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.

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer - Civil Engineering Design, Inc. (Oregon)
United States United States
http://www.civilengdesign.com

ACADEMIC ACHIEVEMENTS:
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]

PROFESSIONAL:
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 Pin
Rev1.011-Mar-14 22:15
Rev1.011-Mar-14 22:15 
Suggestionyou cann't move the file before you close it Pin
Cosmic_Spy11-Mar-14 5:58
Cosmic_Spy11-Mar-14 5:58 
GeneralMy vote of 2 Pin
Sada_moto7-Mar-14 2:28
Sada_moto7-Mar-14 2:28 
QuestionSpecific, and rough Pin
Christian Graus6-Mar-14 18:04
protectorChristian Graus6-Mar-14 18:04 
QuestionUsing is not a good way! Pin
droka19846-Mar-14 17:26
droka19846-Mar-14 17:26 

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

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