2013-07-25 23 views
2

我發現,每次我串接所有文件合併成一個文件,如何刪除按Ctrl-Z與DOS批處理文件

copy *.txt all.txt 

中,有在的末尾按Ctrl-Z字文件。我不想要這個角色。有沒有辦法編寫批處理腳本來刪除這個字符或避免發生這種情況?

我想要做的是

cat *.txt > all.txt 
在linux

回答

2

試試這個:

copy /b *.txt all.txt 

如需幫助,請輸入help copy

+0

你的意思是/ B可以避開那個角色?我看到手冊,它說/ B是指示一個二進制文件。這是什麼意思?爲什麼它可以避免這個問題? –

+0

對不起,我不知道爲什麼,但它的作品。我想,一個文本文件也是一個二進制文件。二進制拷貝意味着'1:1'拷貝,沒有'EOF'追加。 – Endoro

+0

/b僅將一個文件標記爲二進制文件,因此「copy/b * .txt all.txt」表示將* .txt作爲二進制文件打開,然後將它們作爲非二進制文件放入all.txt。要製作目標二進制文件,您需要輸入「copy/b * .txt/b all.txt」。馬庫斯,二進制意味着控制字符。 Ascii文件將刪除所有不可打印的字符。 – Noishe

3

在CP/M天中,文件大小不是以字節記錄,而是128字節的塊。因此,有必要分配文件結束字節(^ Z = 1AH)來指示部分填充的文本文件塊(並且非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 

注意如何juxataposing /a/b產生不同的結果,所有這些結果都與^Z-包含或甚至附加的各種組合有關。

還要注意,如果源是BINARY文件並且省略了/b開關,則簡單的COPY可能會失敗。我從來沒有調查過,但我偶然發現它複製.MPG s。我懷疑如果^Z在任何字節> = 80H之前出現在文件中,那麼該文件被假定爲ASCII,因此在那裏隨便終止 - 但正如我所說的,我沒有調查過它。

+0

+1,很好的概述。 – captcha