views:

28

answers:

1

I am using the MSBuild copy and then Concat to concatenate multiple SQL files. Some of the SQL files are in some other format (opens in notepad), but after Concat gets to the files, there are strange characters all over the resulting files.

Can anyone help me, has anyone experienced this before? How can I convert all SQL files to ascii, or make the Concat task work properly?

+1  A: 

Well I got around it by writing a utility C# class. It is not designed to handle exceptions, they are handled by the calling class or unhandled in the console.

It converts the files to ASCII, removes any invalid characters and rewrites the files.

I spent a while wondering why I was getting access-denied issues, make sure you have all the files you want to convert checked out before you convert them!

Here it is if you are interested:

using System;
using System.IO;
using System.Text;

/// <summary>
/// Manages encoding of text files.
/// </summary>
public class TextFileEncoding
{
    /// <summary>
    /// Main command-line entry.
    /// </summary>
    /// <param name="args"></param>
    public static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            string argParam = args[0].ToString().Trim().ToLower();
            switch (argParam)
            {
                // Return encoding for a file.
                case "get":
                    Encoding enc = GetEncoding(args[1].ToString());
                    Console.WriteLine(enc.EncodingName);
                    break;

                // convert encoding for a path with a given pattern to ASCII.
                case "toascii":
                    int count = ConvertToAscii(args[1].ToString(), args.Length == 3 ? args[2] : "*.*");
                    Console.WriteLine("Successfully converted " + count + " files.");
                    break;
                default:
                    Console.WriteLine("Invalid parameter. Expected get or toascii");
                    break;
            }
        }
        else
        {
            Console.WriteLine("Missing filename parameter.\nFormat: TextFileEncoding.exe [get|toascii] <TextFile> <Path> <Pattern>");
        }
    }

    /// <summary>
    /// Returns the encoding of the filename at the specified path.
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static Encoding GetEncoding(string filename)
    {
        StreamReader fileStream = new StreamReader(filename, true);
        return GetEncoding(fileStream);
    }

    /// <summary>
    /// Returns the encoding of the file stream.
    /// </summary>
    /// <param name="fileStream"></param>
    /// <returns></returns>
    public static Encoding GetEncoding(StreamReader fileStream)
    {
        return fileStream.CurrentEncoding;
    }

    /// <summary>
    /// Converts all files to ascii encoding.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static int ConvertToAscii(string path, string pattern)
    {
        DirectoryInfo pathInfo = new DirectoryInfo(path);
        int count = 0;

        // Get files in directory, recursively.
        FileInfo[] files = pathInfo.GetFiles(pattern, SearchOption.AllDirectories);

        foreach (FileInfo file in files)
        {
            // Encode to ASCII.
            if (EncodeAscii(file))
            {
                count++;
            }
        }

        return count;
    }

    /// <summary>
    /// Converts file to ascii.
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    private static bool EncodeAscii(FileInfo file)
    {
        ASCIIEncoding encoder = new ASCIIEncoding();

        // Get content of file as string.
        string content = File.ReadAllText(file.FullName);

        // Convert to an ASCII byte array.
        byte[] asciiString = encoder.GetBytes(content);

        // Convert to string, so all unknown characters get removed.
        string cleanString = encoder.GetString(asciiString);

        // Convert back to byte array for writing to file.
        byte[] cleanBytes = encoder.GetBytes(cleanString);

        // Delete and rewrite file as ASCII.
        file.Delete();
        File.WriteAllBytes(file.FullName, cleanBytes);

        return true;
    }
}
Russell