① 如何合並當前文件夾下子文件里的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);
}
}
}