2

我发现每次我将所有文件连接成一个文件时

copy *.txt all.txt

,文件末尾有一个 Ctrl-Z 字符。我不要这个角色。有没有办法编写批处理脚本来删除这个字符或避免这种情况发生?

我想做的是

cat *.txt > all.txt 

在linux中。

4

2 回答 2

3

在 CP/M 时代,文件大小不是以字节为单位记录的,而是以 128 字节为单位的 BLOCKS。因此有必要分配一个 END-OF-FILE 字节 (^Z=1AH) 来指示一个部分填充的文本文件块(并且 no-EOF 意味着“完全填充最后的 128 字节块”)

这个约定被 MSDOS 及其衍生产品所尊重 - 这就是为什么COPY CON: filename由 ^Z 终止的原因。

COPY命令允许^Z以不同的方式解释 - 因此/a/b模式,如以下代码所示:

@ECHO OFF
SETLOCAL
:: set COPYCMD=Y to auto-allow overwriting of destination
SET copycmd=Y
:: copy using FRED1.txt (no ^Z-terminal)
>NUL COPY /a fred1.txt freda1.txt
>NUL COPY fred1.txt fred1a.txt /a
>NUL COPY /b fred1.txt fredb1.txt
>NUL COPY fred1.txt fred1b.txt /b
>NUL COPY /a fred1.txt freda1a.txt /a
>NUL COPY /a fred1.txt freda1b.txt /b
>NUL COPY /b fred1.txt fredb1a.txt /a
>NUL COPY /b fred1.txt fredb1b.txt /b

DIR fred*1*|FIND /i "fred"
ECHO.

:: copy using FRED2.txt (has ^Z-terminal) to fred*2*
>NUL COPY /a fred1.txt fred2.txt
>NUL COPY /a fred2.txt freda2.txt
>NUL COPY fred2.txt fred2a.txt /a
>NUL COPY /b fred2.txt fredb2.txt
>NUL COPY fred2.txt fred2b.txt /b
>NUL COPY /a fred2.txt freda2a.txt /a
>NUL COPY /a fred2.txt freda2b.txt /b
>NUL COPY /b fred2.txt fredb2a.txt /a
>NUL COPY /b fred2.txt fredb2b.txt /b

DIR fred*2*|FIND /i "fred"
ECHO.

:: append-copy using FRED1.txt+FRED2.txt to fred*3*
>NUL COPY /a fred1.txt+fred2.txt freda3.txt
>NUL COPY fred1.txt+fred2.txt fred3a.txt /a
>NUL COPY /b fred1.txt+fred2.txt fredb3.txt
>NUL COPY fred1.txt+fred2.txt fred3b.txt /b
>NUL COPY /a fred1.txt+fred2.txt freda3a.txt /a
>NUL COPY /a fred1.txt+fred2.txt freda3b.txt /b
>NUL COPY /b fred1.txt+fred2.txt fredb3a.txt /a
>NUL COPY /b fred1.txt+fred2.txt fredb3b.txt /b

DIR fred*3*|FIND /i "fred"
ECHO.

:: append-copy using FRED2.txt+FRED2.txt to fred*4*
>NUL COPY /a fred2.txt+fred2.txt freda4.txt
>NUL COPY fred2.txt+fred2.txt fred4a.txt /a
>NUL COPY /b fred2.txt+fred2.txt fredb4.txt
>NUL COPY fred2.txt+fred2.txt fred4b.txt /b
>NUL COPY /a fred2.txt+fred2.txt freda4a.txt /a
>NUL COPY /a fred2.txt+fred2.txt freda4b.txt /b
>NUL COPY /b fred2.txt+fred2.txt fredb4a.txt /a
>NUL COPY /b fred2.txt+fred2.txt fredb4b.txt /b

DIR fred*4*|FIND /i "fred"
ECHO.

GOTO :EOF

运行结果:

26/07/2013  08:51                 7 fred1.txt
26/07/2013  08:51                 7 fred1a.txt
26/07/2013  08:51                 7 fred1b.txt
26/07/2013  08:51                 8 freda1.txt
26/07/2013  08:51                 8 freda1a.txt
26/07/2013  08:51                 7 freda1b.txt
26/07/2013  08:51                 7 fredb1.txt
26/07/2013  08:51                 7 fredb1a.txt
26/07/2013  08:51                 7 fredb1b.txt

26/07/2013  08:51                 8 fred2.txt
26/07/2013  08:51                 8 fred2a.txt
26/07/2013  08:51                 8 fred2b.txt
26/07/2013  08:51                 8 freda2.txt
26/07/2013  08:51                 8 freda2a.txt
26/07/2013  08:51                 7 freda2b.txt
26/07/2013  08:51                 8 fredb2.txt
26/07/2013  08:51                 8 fredb2a.txt
26/07/2013  08:51                 8 fredb2b.txt

26/07/2013  09:35                15 fred3a.txt
26/07/2013  09:35                14 fred3b.txt
26/07/2013  09:35                15 freda3.txt
26/07/2013  09:35                15 freda3a.txt
26/07/2013  09:35                14 freda3b.txt
26/07/2013  09:35                15 fredb3.txt
26/07/2013  09:35                16 fredb3a.txt
26/07/2013  09:35                15 fredb3b.txt

26/07/2013  09:35                15 fred4a.txt
26/07/2013  09:35                14 fred4b.txt
26/07/2013  09:35                15 freda4.txt
26/07/2013  09:35                15 freda4a.txt
26/07/2013  09:35                14 freda4b.txt
26/07/2013  09:35                16 fredb4.txt
26/07/2013  09:35                17 fredb4a.txt
26/07/2013  09:35                16 fredb4b.txt

请注意如何并列/a并产生不同的结果,所有这些都带有-inclusion 甚至附加/b的各种组合。^Z

另请注意,COPY如果源是 BINARY 文件并且/b省略了开关,则简单可能会失败。我从来没有调查过它,但我发现它偶尔会发生在复制.MPGs. 我怀疑如果^Z在任何字节 >=80H 之前出现在文件中,则该文件被假定为 ASCII 并因此在那里毫不客气地终止 - 但正如我所说,我没有调查它。

于 2013-07-26T01:48:34.567 回答
2

尝试这个:

copy /b *.txt all.txt

如需帮助,请输入help copy

于 2013-07-25T08:44:35.127 回答