MC ND has a great answer正確處理文件的最後一行可能沒有行結束符(\ n或\ r \ n)的可能性。如果他沒有ECHO(那麼,如果文件的第一行沒有行結束符,那麼文件的第一行可以追加到前一行文件的最後一行.MC ND的答案唯一的問題是它可以增加一個額外的文件之間的空行,即使在不需要的時候。
如果你知道你的所有文件有一個行終止底,然後有一個非常簡單的解決方案,它依賴於無證wildcard characters,而事實上,undocumented wildcards used with FINDSTR do not trigger output of file name prefixes。
@echo off
pushd "c:\your\root\path"
>concatenated.txt 2>nul (for /r /d %%D in (*string*) do findstr "^" "<.txt")
如果您知道某些文件可能缺少最後一行結束符,但所有文件都使用Windows標準\ r \ n行結束符,則可以使用以下內容來有條件地添加li只在需要時纔在文件之間使用ne終止符。 FINDSTR命令檢查文件是否包含不包含\ r(回車)的行。
@echo off
pushd "c:\your\root\path"
>concatenated.txt 2>nul (
for /r /d %%D in (*string*) do for %%F in ("%%D\*.txt) do (
type "%%F"
findstr /v "$" "%%F" >nul && echo(
)
)
如果你知道一些文件可能丟失最後行結束了,你覺得某些文件可能使用的\ n代替\ r \ n,那麼你可以使用我JREPL.BAT regular expression find/replace utility有條件地追加\ r \ n只根據需要。 JREPL.BAT是一個混合的JScript /批處理腳本,可以在任何Windows機器上從本機運行。
@echo off
pushd "c:\your\root\path"
>concatenated.txt 2>nul (
for /r /d %%D in (*string*) do for %%F in ("%%D\*.txt) do (
jrepl "$(?~\r?\n)" "\r\n" /m /x /f "%%F"
)
)
你是什麼意思合併? 你的意思是把它們全部放在一個目錄中,還是你的意思是連接? –
@rep_movsd我想連接它們。我會澄清。 – Hugo