导航:首页 > 文件教程 > 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表格文件化乱码 浏览:986
win7无法打开隐藏文件 浏览:757
怎样把压缩文件变成空白文件 浏览:516
wokrNC和UG编程哪个好 浏览:487
爱奇艺随刻文件名 浏览:40
wps是怎么压缩文件 浏览:986
dos配置文件名 浏览:582
java软件工程师做什么 浏览:897
3dmax导出ive文件 浏览:464
数据重复性好是什么意思 浏览:166
如何查找电脑文件使用历史记录 浏览:821
linux可以运行exe的文件吗 浏览:935
库币是哪个国家的app 浏览:201
大智慧internet文件夹有哪些文件 浏览:89
编程培训课程哪个排名好 浏览:124
我的cad怎么没显示文件选项卡 浏览:952
命令行当前是哪个数据库 浏览:588
为什么appstore打开空白 浏览:574
选货车软件什么app最好 浏览:577
psv仙境传说ace如何降级版本 浏览:460

友情链接