導航:首頁 > 文件教程 > powershell合並文件

powershell合並文件

發布時間:2024-07-10 00:12:47

① 如何合並當前文件夾下子文件里的XML文件

不清楚你的實際文件/情況,僅以問題中的說明及猜測為據;以下代碼復制粘貼到記事本,另存為xx.bat,編碼選ANSI,跟要處理的多個子文件夾放一起雙擊運行
<# :
cls&echo off
rem 將當前目錄里多個子文件夾下的xml文件分別合並為一個文件
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$ext='.xml';
$self=get-item -liter $args[0];
$path=$self.Directory.FullName;
$enc=New-Object System.Text.UTF8Encoding $False;
$folders=@(dir -liter $path|?{$_ -is [System.IO.DirectoryInfo]});
if($folders.length -ge 1){
write-host ('----------------'+$folders[0].Name+'----------------');
$newfile=$path+'\#'+$folders[0].Name+$ext;
$t=New-Object -TypeName System.Collections.ArrayList;
$files=@(dir -liter $folders[0].FullName -recurse|?{($ext -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
for($j=0;$j -lt $files.length;$j++){
write-host ($files[$j].FullName.Substring($path.length));
$text=[IO.File]::ReadAllText($files[$j].FullName,$enc);
[void]$t.add($text.trim());
}
$s=$t -join "`r`n`r`n";
[IO.File]::WriteAllText($newfile,$s,$enc);
}

② powershell中把ABC三個csv格式文件合並成一個並且裡面的內容也全再合成的文件里,這個程序怎麼寫

$allcsv=New-Object system.collections.arraylist
$csvlist=@("a.csv","b.csv","c.csv")
foreach($csv in $csvlist){
ConvertFrom-CSV (gc $csv)|%{$allcsv.add($_)}
}
$allcsv|Export-Csv "allcsv.csv" -not -Enc oem

③ BAT 將當前文件夾中所有文件夾中文件合並到當前文件夾中的第一個文件中,並刪除其餘文件夾

不清楚你的實際文件/情況,僅以問題中的樣例/說明為據;以下代碼復制粘貼到記事本,另存為xx.bat,編碼選ANSI,跟要處理的多個文件夾放一起雙擊運行
<# :
cls&echo off&mode con lines=5000
rem 將當前目錄里除了第一個子文件夾以外的其他子文件夾內的文件剪切/移動/合並到第一個子文件夾里
set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%@% %z%
cd /d "%~dp0"
powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
echo;%#% +%$%%$%/%@% %z%
pause
exit
#>
$codes=@'
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
public static class ExpDir
{
[DllImport("Shlwapi.dll", CharSet=CharSet.Unicode)]
public static extern int StrCmpLogicalW(string p1, string p2);
public static string[] Sort(string[] f)
{
Array.Sort(f, StrCmpLogicalW);
return f;
}
}
'@;
Add-Type -TypeDefinition $codes;

[byte[]]$b=@(32,45,45,62,32);
$c=[Text.Encoding]::Default.GetString($b);
$self=get-item -liter $args[0];
$path=$self.Directory.FullName;
$folders=@(dir -liter $path|?{$_ -is [System.IO.DirectoryInfo]}|%{$_.Name});
if($folders.length -ge 2){
$arr=[ExpDir]::Sort($folders);
for($i=1;$i -lt $arr.count;$i++){
$folder=$path+'\'+$arr[$i];
$files=@(dir -liter $folder -recurse|?{$_ -is [System.IO.FileInfo]});
for($j=0;$j -lt $files.length;$j++){
$newpath=$path+'\'+$arr[0]+$files[$j].Directory.FullName.substring($folder.length);
if(-not (test-path -liter $newpath)){[void][IO.Directory]::CreateDirectory($newpath)};
$newname=$files[$j].Name;
$newfile=$newpath.trimend('\')+'\'+$newname;
$n=2;
while(test-path -liter $newfile){
$newname=$files[$j].BaseName+' ('+$n.toString()+')'+$files[$j].Extension;
$newfile=$newpath.trimend('\')+'\'+$newname;
$n++;
}
$files[$j].FullName.substring($path.length)+$c+$newfile.substring($path.length);
}
}
}

閱讀全文

與powershell合並文件相關的資料

熱點內容
如何給網站掛黑鏈 瀏覽:282
360斷網急救箱網路異常修復不了 瀏覽:889
身份證驗證的js文件 瀏覽:994
什麼人經常換微信名字 瀏覽:707
網站源碼挖掘 瀏覽:944
荔枝fm和app哪個播放量好 瀏覽:535
樂秀音頻在哪個文件里 瀏覽:486
以數據說話什麼意思 瀏覽:319
java中對象 瀏覽:794
酷狗網路機頂盒如何設置 瀏覽:408
threejs添加文字 瀏覽:436
微信春節理財通 瀏覽:948
qq主題模塊應用教程 瀏覽:993
javaweb項目打包 瀏覽:59
qq主題是哪個文件夾里 瀏覽:642
超越狂暴升級txt書包 瀏覽:263
cad復制不能粘貼到新建文件 瀏覽:565
linux網路socket編程 瀏覽:926
戰爭雷霆客戶端在哪個文件里 瀏覽:847
如何授權點淘app讀取步數 瀏覽:671

友情鏈接