批处理脚本从文件中删除BOM() [英] Batch script remove BOM () from file

查看:46
本文介绍了批处理脚本从文件中删除BOM()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个批处理脚本,将SQL文件从一个文件夹复制到一个大的SQL脚本中.问题是,当我运行这个SQL脚本时,它会显示错误

I have created a batch script to copy SQL files from a folder into one big SQL script. The problem is when I run this one SQL script it comes up with the error

"附近的语法不正确

Incorrect syntax near ''

我将一个SQL脚本复制到Notepad ++中,并将编码设置为ANSI.我在发生错误的行上看到该符号(BOM).

I copied the one SQL script into Notepad++ and set the encoding to ANSI. I see this symbol  (BOM) on the lines where the error is happening.

无论如何,我可以在批处理脚本中自动将其删除.我不想每次运行此任务时都手动删除它.

Is there anyway I can automatically remove this within my batch script. I don't want to keep manually remove this every time I run this task.

下面是我当前拥有的批处理脚本

Below is the batch script I have currently

@echo off

set "path2work=C:\StoredProcedures"
cd /d "%path2work%"

echo. > C:\FinalScript\AllScripts.sql

for %%a in (*.sql) do (

    echo. >>"C:\FinalScript\AllScripts.sql"
    echo GO >>"C:\FinalScript\AllScripts.sql"
    type "%%a">>"C:\FinalScript\AllScripts.sql"
    echo. >>"C:\FinalScript\AllScripts.sql"
)

推荐答案

这是因为 type 命令将保留UTF-8 BOM,因此当您组合多个具有BOM的文件时,最终文件将在文件中间的不同位置包含多个BOM.

This is because the type command will preserve the UTF-8 BOM, so when you combine multiple files which have the BOM, the final file will contain multiple BOMs in various places in middle of the file.

如果确定要合并的所有SQL文件均以BOM表开头,则可以在实际合并它们之前使用以下脚本从每个脚本中删除BOM表.

If you are certain that all the SQL files that you want to combine, start with the BOM, then you can use the following script to remove the BOM from each of them before actually combining them.

这是通过管道传输 type 的输出来完成的.在3条 pause 命令的帮助下,管道的另一端将占用前3个字节(BOM).每个 pause (暂停)将占用一个字节.流的其余部分将发送到 findstr 命令,以将其附加到最终脚本中.

This is done by piping the output of type. The other side of pipe will consume the first 3 bytes (The BOM) with the help of 3 pause commands. each pause will consume one byte. The rest of stream will be send to the findstr command to append it to final script.

由于SQL文件已编码为UTF-8,并且它们可能包含Unicode范围内的任何字符,所以某些代码页会干扰操作,并可能导致最终的SQL脚本损坏.

Since the SQL files are encoded UTF-8 and they may contain any characters in the Unicode range, certain code pages will interfere with the operation and may cause the final SQL script to be corrupted.

因此已将其考虑在内,批处理文件将使用代码页437重新启动,该代码页可安全访问任何二进制序列.

So this has been taken into account and the batch file will be restarted with code page 437 which is safe for accessing any binary sequence.

@echo off
setlocal DisableDelayedExpansion


setlocal EnableDelayedExpansion
for /F "tokens=*" %%a in ('chcp') do for %%b in (%%a) do set "CP=%%~nb"
if  !CP! NEQ 437 if !CP! NEQ 65001 chcp 437 >nul && (

    REM for file operations, the script must restatred in a new instance.
    "%COMSPEC%" /c "%~f0"

    REM Restoring previous code page
    chcp !CP! >nul
    exit /b
)
endlocal


set "RemoveUTF8BOM=(pause & pause & pause)>nul"
set "echoNL=echo("
set "FinalScript=C:\FinalScript\AllScripts.sql"

:: If you want the final script to start with UTF-8 BOM (This is optional)
:: Create an empty file in NotePad and save it as UTF8-BOM.txt with UTF-8 encoding.
:: Or Create a file in your HexEditor with this byte sequence: EF BB BF
:: and save it as UTF8-BOM.txt
:: The file must be exactly 3 bytes with the above sequence.
(
    type "UTF8-BOM.txt" 2>nul

    REM This assumes that all sql files start with UTF-8 BOM
    REM If not, then they will loose their first 3 otherwise legitimate characters.
    REM Resulting in a final corrupted script.
    for %%A in (*.sql) do (type "%%~A" & %echoNL%)|(%RemoveUTF8BOM% & findstr "^")

)>"%FinalScript%"

这篇关于批处理脚本从文件中删除BOM()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆