导航:首页 > 文件教程 > 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合并文件相关的资料

热点内容
专题学习网站源码 浏览:163
jsphead什么 浏览:88
gps串口数据怎么发送 浏览:968
win10文件主页共享查看 浏览:411
中国联通有哪些app是免流的 浏览:176
边做边保存的文件找不到了 浏览:858
win10照片应用文件夹名称 浏览:966
编程如何解决资金的原子性 浏览:638
如何制作广角镜头矫正文件 浏览:513
在网页开发中应该选用哪个数据库 浏览:742
iphone5移动卡贴 浏览:990
电脑文件的格式 浏览:127
extjs的xtype 浏览:959
suse11iso文件要u盘安装 浏览:153
如何将报表统计数据转化为图形 浏览:444
如何寄快递材料文件 浏览:265
java构造方法private 浏览:475
手机文件找回恢复 浏览:516
word怎么把u盘里的文件拔掉 浏览:976
港版苹果用的插排 浏览:1000

友情链接