2023年10月23日 星期一

在BAT中如何做 Ascii 文本的變換 更換換行符號 (Win -> Linux)

在BAT中如何做 Ascii 文本的變換 更換換行符號 (Win -> Linux)

這是上一篇的延續 CHG: 在BAT中如何做 Ascii 文本的變換 更換換行符號 (Linux -> Win)

這邊就不多作介紹了,補上反過來的 Dos2unix 的函式,剩下自己參考上一篇

原始的 C# 函式 Dos2unix

using System.IO;
public class EOLConverter
{
    public static void Dos2unix(string sourcePath, string destinationPath)
    {
        const int readBufferSize = 16384;
        byte[] readBuffer = new byte[readBufferSize];
        byte[] writeBuffer = new byte[readBufferSize];
        using (FileStream readStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
        using (FileStream writeStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
        {
            int bytesRead;
            while ((bytesRead = readStream.Read(readBuffer, 0, readBufferSize)) > 0)
            {
                int writeIndex = 0;
                for (int i = 0; i < bytesRead; i++)
                {
                    if (readBuffer[i] != 13)
                    {
                        writeBuffer[writeIndex++] = readBuffer[i];
                    }
                }
                writeStream.Write(writeBuffer, 0, writeIndex);
            }
        }
    }
}



寫成一行的 bat

@echo off& Title EOLConverter By Charlotte
set "EOLConverter.cs=using System.IO; public class EOLConverter { public static void Dos2unix(string sourcePath, string destinationPath) { const int readBufferSize = 16384; byte[] readBuffer = new byte[readBufferSize]; byte[] writeBuffer = new byte[readBufferSize]; using (FileStream readStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read)) using (FileStream writeStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write)) { int bytesRead; while ((bytesRead = readStream.Read(readBuffer, 0, readBufferSize)) > 0) { int writeIndex = 0; for (int i = 0; i < bytesRead; i++) { if (readBuffer[i] != 13) { writeBuffer[writeIndex++] = readBuffer[i]; } } writeStream.Write(writeBuffer, 0, writeIndex); } } } }"
echo data\CRLF.txt| powershell -nop "Add-Type '%EOLConverter.cs%'; [string]$SrcPath=$input; $DstPath=$SrcPath+'.tmp'; [EOLConverter]::Dos2unix($SrcPath, $DstPath); #Move-Item -Path $DstPath -Destination $SrcPath -Force -ErrorAction Stop"
exit /b %errorlevel%



下面是完整的函式結合兩篇的 PowerShell

$EOLConverter = @"
using System.IO;
public class EOLConverter
{
    public static void Unix2dos(string sourcePath, string destinationPath)
    {
        const int readBufferSize = 16384;
        byte[] readBuffer = new byte[readBufferSize];
        byte[] writeBuffer = new byte[readBufferSize * 2];
        byte previousByte = 0;
        using (FileStream readStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
        using (FileStream writeStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
        {
            int bytesRead;
            while ((bytesRead = readStream.Read(readBuffer, 0, readBufferSize)) > 0)
            {
                int writeIndex = 0;
                for (int i = 0; i < bytesRead; i++)
                {
                    if (readBuffer[i] == 10 && previousByte != 13)
                    {
                        writeBuffer[writeIndex++] = 13;
                    }
                    writeBuffer[writeIndex++] = readBuffer[i];
                    previousByte = readBuffer[i];
                }
                writeStream.Write(writeBuffer, 0, writeIndex);
            }
        }
    }

    public static void Dos2unix(string sourcePath, string destinationPath)
    {
        const int readBufferSize = 16384;
        byte[] readBuffer = new byte[readBufferSize];
        byte[] writeBuffer = new byte[readBufferSize];
        using (FileStream readStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read))
        using (FileStream writeStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write))
        {
            int bytesRead;
            while ((bytesRead = readStream.Read(readBuffer, 0, readBufferSize)) > 0)
            {
                int writeIndex = 0;
                for (int i = 0; i < bytesRead; i++)
                {
                    if (readBuffer[i] != 13)
                    {
                        writeBuffer[writeIndex++] = readBuffer[i];
                    }
                }
                writeStream.Write(writeBuffer, 0, writeIndex);
            }
        }
    }
}
"@

Add-Type -TypeDefinition $EOLConverter
[EOLConverter]::Unix2dos("data\LF.txt", "tmp\CRLF.txt")
[EOLConverter]::Dos2unix("tmp\CRLF.txt", "tmp\LF.txt")






沒有留言:

張貼留言