27
Jul/09
0

Remove Blank Lines from a Text File

I ran into a tricky Problem the other day. I had to remove the terminating blank lines in a CSV File as it was parsed by another program which did not handle the case of blank lines and thus failed. I figured this is not easy to do with a simple batch file so I created a little C# Program that does the trick.

The Program can be run from a command line and requires the path to the Text File where you want to remove the ending blank lines from.

Download Compiled Version: RemoveBlankLines

C# Code:

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = args[0];
            if (file == "")
            {
                Console.WriteLine("Please enter the path to the File!");
                Environment.Exit(1);
            }
            if (!File.Exists(file))
            {
                Console.WriteLine("Could not find File {0}!", file);
                Environment.Exit(1);
            }
 
            RemoveBlankRowsFromCSVFile(file, 0);
            RemoveBlankRowsFromCSVFile(file, CountLines(file));
        }
 
        public static void RemoveBlankRowsFromCSVFile(string filepath, int lastline)
        {
            if (filepath == null || filepath.Length == 0)
            {
                throw new ArgumentNullException("filepath");
            }
 
            if (!File.Exists(filepath))
            {
                throw new FileNotFoundException("Could not find file.", filepath);
            }
 
            String tempFile = Path.GetTempFileName();
            int linecount = 0;
 
            using (StreamReader reader = new StreamReader(filepath))
            using (StreamWriter writer = new StreamWriter(tempFile))
            {
                String line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    linecount++;
 
                    line = line.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
                    if (!line.Equals(String.Empty))
                    {
                        if ((linecount >= lastline) && (lastline != 0))
                            writer.Write(line);
                        else
                            writer.WriteLine(line);
                    }
                }
 
            }
 
            File.Delete(filepath);
            File.Move(tempFile, filepath);
        }
 
        static int CountLines(string file)
        {
            int count = 0;
            using (StreamReader r = new StreamReader(file))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    count++;
                }
            }
            return count;
        }
 
 
    }
}
Print This Post
(8 votes, average: 4.63 out of 5)
Loading ... Loading ...
2,149 views
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


No trackbacks yet.