作業系統:Windows 10
這是示例目錄結構。
D:\Fruits\Apple\ a.txt k.txt c.txt
D:\Fruits\Mango\ g.txt q.txt b.txt
我想像這樣合并每個檔案夾的txt檔案。
D:\Fruits\Apple\ a.txt k.txt c.txt merge.txt (a k c)
D:\Fruits\Mango\ g.txt q.txt b.txt merge.txt (g q b)
@echo 關閉
對于 /r " D:\Fruits" %%a in (*.txt) 輸入 "%%a" >>"merge.txt"
我嘗試了這批,但結果不是我的預期。
D:\Fruits\merge.txt (a k c g q b)
請幫助我如何繼續執行任務。謝謝你。
uj5u.com熱心網友回復:
用于for /D遍歷目錄,然后單獨處理每個目錄,dir用于檢索所有*.txt檔案、findstr排除結果檔案merge.txt并for /F遍歷檔案。要最終寫入merge.txt,請使用輸出重定向>:
for /D %%J in ("D:\Fruits\*") do (
> "%%~J\merge.txt" (
for /F "delims= eol=|" %%I in ('
dir /B /A:-D-H-S /O:N "%%~J\*.txt" ^| findstr /V /I /C:"merge.txt"
') do (
type "%%~J\%%I"
)
)
)
代替type寫入結果檔案,copy也可以使用:
for /D %%J in ("D:\Fruits\*") do (
> "%%~J\merge.txt" rem/ // deplete the file in advance;
for /F "delims= eol=|" %%I in ('
dir /B /A:-D-H-S /O:N "%%~J\*.txt" ^| findstr /V /I /C:"merge.txt"
') do (
copy /B "%%~J\merge.txt" "%%~J\%%I" "%%~J\merge.txt"
)
)
由于命令的*.txt原因,檔案按字母順序合并。/O:Ndir
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/410643.html
標籤:
