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

热点内容
智能ai机器人需要什么编程 浏览:825
可控编程主要技术指标有哪些 浏览:271
数据分析回归需要什么条件 浏览:285
微信小程序按钮颜色 浏览:69
长江大学网课用什么app 浏览:431
华中系统图纸编程哪个刀好 浏览:38
地方债务数据在哪里查看 浏览:932
扫描文件怎么设置格式 浏览:957
苹果邮箱主机名填什么 浏览:630
多张图片同一个文件夹 浏览:798
win7怎么打开shs文件 浏览:481
怎么把文件夹做成iso 浏览:164
缤客网站上的房价怎么在哪里修改 浏览:406
单片机c51计数器实验代码 浏览:990
宏编程鼠标代表什么意思 浏览:753
别人捡到苹果6有用吗 浏览:829
word文件用wps打开 浏览:477
macbook修改文件格式软件 浏览:757
美版s7edge那个版本好 浏览:529
视频隐藏在文件夹里 浏览:144

友情链接