(view source code of utf8removebom.cs as plain text)
namespace RobvanderWoude{ class UTF8removeBOM {static string progver = "1.00";
static int Main( string[] args )
{ #region Parse Command Lineif ( args.Length < 2 || args.Length > 3 )
{return ShowHelp( );
}bool overwrite = false;
string filein = args[0];
string fileout = args[0];
if ( !System.IO.File.Exists( filein ) )
{return ShowHelp( "Source file not found" );
}if ( args[1].ToUpper( ) == "/O" )
{overwrite = true;
} else {fileout = args[1];
}if ( args.Length == 3 )
{if ( args[2].ToUpper( ) == "/O" )
{overwrite = true;
} else {fileout = args[2];
} }if ( !overwrite )
{if ( filein == fileout )
{return ShowHelp( "Use /O to overwrite existing source file" );
} else {if ( System.IO.File.Exists( fileout ) )
{return ShowHelp( "Use /O to overwrite existing target file" );
} } } #endregion Parse Command Line try { // Open source fileSystem.IO.StreamReader sr = new System.IO.StreamReader( filein );
// Read text from source filestring text = sr.ReadToEnd( );
// Close source filesr.Close( );
// Delete target file if it existsif ( System.IO.File.Exists( fileout ) )
{System.IO.File.Delete( fileout );
} // Open target file, set encoding to UTF-8 without BOMSystem.IO.StreamWriter sw = new System.IO.StreamWriter( fileout, false, new System.Text.UTF8Encoding( false ) );
// Write text to target filesw.Write( text );
// Close target filesw.Close( );
}catch ( System.Exception e )
{return ShowHelp( e.Message );
} // Donereturn 0;
} #region Error handlingpublic static int ShowHelp( params string[] errmsg )
{ #region Error Messageif ( errmsg.Length > 0 )
{System.Collections.Generic.List<string> errargs = new System.Collections.Generic.List<string>( errmsg );
errargs.RemoveAt( 0 );
System.Console.Error.WriteLine( );
System.Console.ForegroundColor = System.ConsoleColor.Red;
System.Console.Error.Write( "ERROR:\t" );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
System.Console.ResetColor( );
} #endregion Error Message #region Help Text /* UTF8removeBOM, Version 1.00 Read a text file and save the text in UTF-8 format without Byte Order Mark Usage: UTF8REMOVEBOM textfilein [ texfileout ] [ /O ] Where: textfilein is the source text file textfileout is the target text file (default if not specified: textfilein) /O overwrite existing target file, required if target file is not specified (default: abort if target file exists) Notes: At least 2 command line arguments are required. Return code is -1 in case of errors, otherwise 0. This simple tool was written with the following workflow in mind: Use VBScript's OpenTextFile with TriStateTrue option to write text file Use this tool to remove the Byte Order Mark Use PHP command print( htmlentities( file_get_contents( textfile ) ) ) to show the text correctly in a web page Written by Rob van der Woude http://www.robvanderwoude.com */ #endregion Help Text #region Display helpSystem.Console.Error.WriteLine( );
System.Console.Error.WriteLine( "UTF8removeBOM.exe, Version {0}", progver );
System.Console.Error.WriteLine( "Read a text file and save the text in UTF-8 format without Byte Order Mark" );
System.Console.Error.WriteLine( );
System.Console.Error.Write( "Usage: " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.WriteLine( "UTF8REMOVEBOM textfilein [ texfileout ] [ /O ]" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( );
System.Console.Error.Write( "Where: " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( "textfilein" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " is the source text file" );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( " textfileout" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " is the target text file" );
System.Console.Error.WriteLine( " (default if not specified: textfilein)" );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( " /O" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " overwrite existing target file," );
System.Console.Error.WriteLine( " required if target file is not specified" );
System.Console.Error.WriteLine( " (default: abort if target file exists)" );
System.Console.Error.WriteLine( );
System.Console.Error.WriteLine( "Notes: At least 2 command line arguments are required." );
System.Console.Error.WriteLine( " Return code is -1 in case of errors, otherwise 0." );
System.Console.Error.WriteLine( " This simple tool was written with the following workflow in mind:" );
System.Console.WriteLine( );
System.Console.Error.Write( " * VBScript's " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( "OpenTextFile" );
System.Console.ResetColor( );
System.Console.Error.Write( " writes text file with " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( "TriStateTrue" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " option" );
System.Console.Error.Write( " * " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.Write( "UTF8removeBOM.exe" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " removes the Byte Order Mark" );
System.Console.Error.Write( " * PHP command " );
System.Console.ForegroundColor = System.ConsoleColor.White;
System.Console.Error.WriteLine( "print( htmlentities( file_get_contents( textfile ) ) )" );
System.Console.ResetColor( );
System.Console.Error.WriteLine( " shows the text correctly in a web page" );
System.Console.Error.WriteLine( );
System.Console.Error.WriteLine( "Written by Rob van der Woude" );
System.Console.Error.WriteLine( "http://www.robvanderwoude.com" );
#endregion Display Helpreturn -1;
} #endregion Error handling }}page last modified: 2025-10-11; loaded in 0.0082 seconds