powershell 文件内容合并




1
# 合并目录下的所有文件,并以UTF8格式保存到指定文件 2 function funcCombinFilesInUTF8($fileDir, $destFile) 3 { 4 5 echo ‘‘ | out-file -Encoding utf8 -filepath $destFile 6 $fileList = Get-ChildItem $fileDir 7 8 Foreach($file in $fileList) 9 { 10 get-content $file.fullname | out-file -append -Encoding utf8 $destFile 11 echo ‘‘ | out-file -append -Encoding utf8 $destFile 12 } 13 14 } 15 16 function funcCombinFiles($fileDir, $destFile) 17 { 18 echo ‘‘ > $destFile 19 $fileList = Get-ChildItem $fileDir 20 21 Foreach($file in $fileList) 22 { 23 get-content $file.fullname >> $destFile 24 echo ‘‘ >> $destFile 25 } 26 27 } 28 29 funcCombinFilesInUTF8 C:\Users\shadow\Desktop\tmp C:\Users\shadow\Desktop\tmp_all.txt 30 funcCombinFiles C:\Users\shadow\Desktop\tmp C:\Users\shadow\Desktop\tmp_all2.txt

改进后的脚本:

 1 #文件夹和输入文件件
 2 $fromDir=D:\.weibo_chat.tar\txt\‘
 3 $saveToDir=D:\.weibo_chat.tar\txt\合并后的文本\‘
 4 
 5 
 6 
 7 
 8 if(-not (Test-Path $fromDir)){
 9   echo 请输入正确的文件所在路径!  
10   return
11 }
12 
13 if(-not (Test-Path $saveToDir)){
14   mkdir $saveToDir
15 }
16 
17 function funcCombinFiles($fileDir, $destFile)
18 {
19     echo ‘‘ > $destFile
20     $fileList = Get-ChildItem -Depth 1  $fileDir -r  *.txt
21 
22     Foreach($file in $fileList)
23     {
24        get-content $file.fullname >> $destFile
25        echo ‘‘ >> $destFile
26     }
27 
28 }
29 
30 #输出文件名
31 $now= Get-Date -Format yyyy-MM-dd-HH-mm-ss
32 $fileName =$saveToDir+$now+.rtf
33 
34 funcCombinFiles $fromDir  $fileName

 

powershell 文件内容合并

上一篇:php读取sqlite数据库入门实例


下一篇:linux修改文件所有者和文件所在组