powershell 文件内容合并

# 合并目录下的所有文件,并以UTF8格式保存到指定文件
function funcCombinFilesInUTF8($fileDir, $destFile)
{

    echo ‘‘ | out-file -Encoding utf8 -filepath $destFile
    $fileList = Get-ChildItem  $fileDir

    Foreach($file in $fileList)
    {
       get-content $file.fullname | out-file -append -Encoding utf8  $destFile
       echo ‘‘ | out-file -append -Encoding utf8 $destFile
    }

}

function funcCombinFiles($fileDir, $destFile)
{
    echo ‘‘ > $destFile
    $fileList = Get-ChildItem  $fileDir

    Foreach($file in $fileList)
    {
       get-content $file.fullname >> $destFile
       echo ‘‘ >> $destFile
    }

}

funcCombinFilesInUTF8 ‘C:\Users\shadow\Desktop\tmp‘ ‘C:\Users\shadow\Desktop\tmp_all.txt‘
funcCombinFiles ‘C:\Users\shadow\Desktop\tmp‘ ‘C:\Users\shadow\Desktop\tmp_all2.txt‘

改进后的脚本:

#文件夹和输入文件件
$fromDir=‘D:\.weibo_chat.tar\txt\‘
$saveToDir=‘D:\.weibo_chat.tar\txt\合并后的文本\‘




if(-not (Test-Path $fromDir)){
  echo ‘请输入正确的文件所在路径!‘  
  return
}

if(-not (Test-Path $saveToDir)){
  mkdir $saveToDir
}

function funcCombinFiles($fileDir, $destFile)
{
    echo ‘‘ > $destFile
    $fileList = Get-ChildItem -Depth 1  $fileDir -r  *.txt

    Foreach($file in $fileList)
    {
       get-content $file.fullname >> $destFile
       echo ‘‘ >> $destFile
    }

}

#输出文件名
$now= Get-Date -Format ‘yyyy-MM-dd-HH-mm-ss‘
$fileName =$saveToDir+$now+‘.rtf‘

funcCombinFiles $fromDir  $fileName