本帖最后由 zaqmlp 于 2021-4-28 19:48 编辑
- <# :
- cls&echo off&mode con lines=3000
- rem 代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起双击运行
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
- 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;
-
- $outfile=".\汇总.txt";
- $self=get-item -liter $args[0];
- $path=$self.Directory.FullName;
- $tn=$outfile -replace '^.*\\','';
- $arr=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_.Name -ne $tn) -and ($_ -is [System.IO.FileInfo])});
- if($arr.length -ge 1){
- for($i=0;$i -lt $arr.length;$i++){
- move-item -liter $arr[$i].FullName ($arr[$i].Directory.FullName+'\#'+$arr[$i].Name);
- }
- $num=5;
- $enc=New-Object System.Text.UTF8Encoding $False;
- $fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
- $sw=New-Object System.IO.StreamWriter($fs, $enc);
- $brr=@($arr|%{$_.Name});
- $crr=[ExpDir]::Sort($brr);
- for($i=0;$i -lt $crr.Count;$i++){
- $ext='';
- $m=[regex]::match($crr[$i],'\.[^\.]+$');
- if($m.Success){$ext=$m.groups[0].value;}
- $oldfile=$path+'\#'+$crr[$i];
- $newname=($i+1).toString()+$ext;
- $newfile=$path+'\'+$newname;
- write-host ($crr[$i]+' --> '+$newname);
- $text=[IO.File]::ReadAllLines($oldfile,$enc);
- for($j=0;$j -lt $num;$j++){
- if($j -lt $text.count){
- if($text[$j].trim() -ne ''){
- $line=($i+1).toString()+'----'+$text[$j];
- $sw.WriteLine($line);
- $sw.Flush();
- break;
- }
- }
- }
- move-item -liter $oldfile $newfile;
- }
- $sw.Close();
- $fs.Close();
- }
复制代码
|