導航:首頁 > 文件教程 > 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合並文件相關的資料

熱點內容
excel製作的文件顯示灰色 瀏覽:828
手機存儲數據種類有哪些 瀏覽:447
matlab神經網路應用設計源代碼 瀏覽:444
壓縮文件怎麼發送給微信朋友 瀏覽:101
中文密碼怎麼解壓 瀏覽:911
h格式的文件 瀏覽:744
vary安裝教程 瀏覽:156
復制文件夾裡面所有文件名 瀏覽:836
java怎麼給字元串排序 瀏覽:405
java數組類 瀏覽:533
excel文件目錄中文名都顯示問號 瀏覽:84
js分行寫 瀏覽:419
三星首選網路如何設置 瀏覽:867
java讀取config文件 瀏覽:795
蘋果無法指紋支付密碼 瀏覽:63
怎麼查看工作目錄里的文件名 瀏覽:525
華為黃app怎麼下載 瀏覽:342
禁用U盤怎麼傳輸文件 瀏覽:88
wifi萬能密碼舊版 瀏覽:340
linux如何傳文件虛擬機 瀏覽:929

友情鏈接