2016-03-15 122 views
0

我在單個父文件夾內有50個子文件夾。合併父文件夾中不同文件夾中的文本文件

每個子文件夾中都有多個.txt文件。我想將單個子文件夾中的所有文本文件合併爲1個.txt文件。

但我想要一個命令,以便可以一次完成所有子文件夾,就像我不想爲每個子文件夾寫入命令一樣。

例如: -

ABCD(父文件夾): - 甲 B;這裏A和B是子文件夾

A \ 0001.txt A \ 0002.txt

我想合併,使一個單一的文本文件\ 0001.txt。

乙\ 0001.txt 乙\ 0002.txt

我想合併這兩個B中文件夾中的文本文件。

它可以一氣呵成嗎?

回答

0

這可能使用PowerShell更容易。

請嘗試以下操作並將basedir更改爲所有子目錄的父文件夾。

$basedir = "C:\Basedir" 
$folderlist = Get-childitem -Path $basedir 
foreach ($folder in $folderlist) 
{ 
$dir = $folder 
$outFile = Join-Path $dir "merged.txt" 
# Build the file list 
$fileList = Get-ChildItem -Path $dir -Filter File*.txt -File 
# Get the header info from the first file 
Get-Content $fileList[0] | select -First 2 | Out-File -FilePath $outfile -Encoding ascii 
# Cycle through and get the data (sans header) from all the files in the list 
foreach ($file in $filelist) 
{ 
Get-Content $file | select -Skip 2 | Out-File -FilePath $outfile -Encoding ascii -Append 
} 
} 
相關問題