Remove multiple line in the same file with C#

Read the file, remove the multiple line (but it saves one of them) in memory and put the contents back to the file (overwriting) and create a backup file with the original file.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DelMultiLine {
   class Program {
      static void Main(string[] args) {
         if (args.Count() == 0 || args.Count() > 2) {
            Console.WriteLine("\nDelMultiLine (by Enrico Rossini -");
            Console.WriteLine("Remove duplicate line in a file\n");
            Console.WriteLine("   delmultiline <Filename> <resultFilename>\n");
            Console.WriteLine("filename:       define a full path for the file you want to elaborate");
            Console.WriteLine("resultFilename: define the full path for the original file for a backup");

         string file1 = args[0];
         string file2 = "";

         if (args.Count() == 1) {
            if (string.IsNullOrEmpty(file2)) {
               file2 = file1 + ".old";
            else {
               file2 = args[1];

         Console.WriteLine(string.Format("Reading {0} in progress...", args[0]));
         string[] lines = File.ReadAllLines(file1);
         List<string> newline = new List<string>();

         for (int i = 0; i < lines.Length; i++) {

         Console.WriteLine("Deleting multiple line in progress...");
         for (int i = 0; i < lines.Length; i++) {
            List<string> temp = new List<string>();
            int duplicate_count = 0;

            for (int j = newline.Count - 1; j >= 0; j--) {
               //checking for duplicate records
               if (lines[i] != newline[j])
               else {
                  if (duplicate_count == 1)
            newline = temp;

         // reverse the array

         //assigning into a string array
         string[] newFile = newline.ToArray();

         // move the original file in a new location
         Console.WriteLine(string.Format("Copying original file in {0}", args[0]));
         File.Move(file1, file2);

         //now writing the data to a text file
         Console.WriteLine(string.Format("Write new file {0}", args[0]));
         File.WriteAllLines(file1, newFile);

         Console.WriteLine("Convertion is finished.");
         Console.WriteLine("\nPress any key to continue...");

Happy coding!

Change a character in a list of list with PowerShell

How do I delete certain characters or replace certain characters with other characters by some batch file execution, for filenames of all files in a Windows folder in one go, is there a PowerShell command for that?

I've shown how to batch rename all the files and directories in the current directory that contain spaces by replacing them with _ underscores.

Dir | Rename-Item -NewName { $_.Name -replace "-", "_" }

Optionally, the Where-Object command can be used to filter out ineligible objects for the successive cmdlet (command-let). The following are some examples to illustrate the flexibility it can afford you:

To skip any document files

Dir |
Where-Object { $_.Name -notmatch "\.(doc|xls|ppt)x?$" } |
Rename-Item -NewName { $_.Name -replace " ","_" }

To process only directories (pre-3.0 version)

Dir |
Where-Object { $_.Mode -match "^d" } |
Rename-Item -NewName { $_.Name -replace " ","_" }

PowerShell v3.0 introduced new Dir flags. You can also use Dir -Directory there.

To skip any files already containing an underscore (or some other character)

Dir |
Where-Object { -not $_.Name.Contains("_") } |
Rename-Item -NewName { $_.Name -replace " ","_" }

Happy coding!