Board logo

标题: [文本处理] [已解决]批处理怎样按序修改所有文件名并提取首行汇总? [打印本页]

作者: chounan    时间: 2021-4-28 07:03     标题: [已解决]批处理怎样按序修改所有文件名并提取首行汇总?

求高手写个角本,好使给红包哈
要求,目录下有多个txt文件,名字乱,不规范,先批量按序修改所有文件名称并对应记录到txt文件中,如:改成、.txt、2.txt、3.txt、。。。。。
改完后:根据改完的txt文档,取每个文档名称和文档内容首行做为数据存到汇总文件中:汇总文件里的内容为每一行对应一个文档名称与内容首行:如1---(1.txt的第一行内容),2---(2.txt的第一行内容)。。。。。
重要的是每个文档如果第一行为空无数据则取第二行。第二行没有取第三行,直到取到第五行,还没有数据则不取值了。
有难过不知道有没有能力写出来。


二楼很历害。好评好评,zaqmlp
作者: zaqmlp    时间: 2021-4-28 10:55

本帖最后由 zaqmlp 于 2021-4-28 19:48 编辑
  1. <# :
  2. cls&echo off&mode con lines=3000
  3. rem 代码复制粘贴到记事本,另存为xx.bat,编码选ANSI,跟要处理的文件放一起双击运行
  4. cd /d "%~dp0"
  5. powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::GetEncoding('GB2312')))) -Args '%~f0'"
  6. pause
  7. exit
  8. #>
  9. $codes=@'
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Runtime.InteropServices;
  13. public static class ExpDir
  14. {
  15.     [DllImport("Shlwapi.dll", CharSet=CharSet.Unicode)]
  16.     public static extern int StrCmpLogicalW(string p1, string p2);
  17.     public static string[] Sort(string[] f)
  18.     {
  19.         Array.Sort(f, StrCmpLogicalW);
  20.         return f;
  21.     }  
  22. }
  23. '@;
  24. Add-Type -TypeDefinition $codes;
  25. $outfile=".\汇总.txt";
  26. $self=get-item -liter $args[0];
  27. $path=$self.Directory.FullName;
  28. $tn=$outfile -replace '^.*\\','';
  29. $arr=@(dir -liter $path|?{('.txt' -eq $_.Extension) -and ($_.Name -ne $tn) -and ($_ -is [System.IO.FileInfo])});
  30. if($arr.length -ge 1){
  31. for($i=0;$i -lt $arr.length;$i++){
  32. move-item -liter $arr[$i].FullName ($arr[$i].Directory.FullName+'\#'+$arr[$i].Name);
  33. }
  34. $num=5;
  35. $enc=New-Object System.Text.UTF8Encoding $False;
  36. $fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
  37. $sw=New-Object System.IO.StreamWriter($fs, $enc);
  38. $brr=@($arr|%{$_.Name});
  39. $crr=[ExpDir]::Sort($brr);
  40. for($i=0;$i -lt $crr.Count;$i++){
  41. $ext='';
  42. $m=[regex]::match($crr[$i],'\.[^\.]+$');
  43. if($m.Success){$ext=$m.groups[0].value;}
  44. $oldfile=$path+'\#'+$crr[$i];
  45. $newname=($i+1).toString()+$ext;
  46. $newfile=$path+'\'+$newname;
  47. write-host ($crr[$i]+' --> '+$newname);
  48. $text=[IO.File]::ReadAllLines($oldfile,$enc);
  49. for($j=0;$j -lt $num;$j++){
  50. if($j -lt $text.count){
  51. if($text[$j].trim() -ne ''){
  52. $line=($i+1).toString()+'----'+$text[$j];
  53. $sw.WriteLine($line);
  54. $sw.Flush();
  55. break;
  56. }
  57. }
  58. }
  59. move-item -liter $oldfile $newfile;
  60. }
  61. $sw.Close();
  62. $fs.Close();
  63. }
复制代码

作者: chounan    时间: 2021-4-28 19:42

回复 2# zaqmlp


    执行完汇里是乱码:如下图


1----10:31鐭?
2----绂忓僵鍜屼綋褰?
3----3D璁ㄨ缇?
4----G21鎯犲伐鏂扮敓鍜ㄨ缇?
5----杈藉畞骞夸笢鎬诲喅璧涚悆杩峰鍠风兢
6----浠?s
7----<
8---- 4G OK/s (12)
9----浠婃棩浣撹偛
10----濮獩浜ゆ祦浼?
11----Ps瑙嗛鍓緫CAD瀛︿範缇?6
12----10:25
13----浣犵湅瑙佹垜鐨勭尗浜嗕箞
14----鐢靛瓙鐖卞ソ鑰呯ぞ鍥?
15----鈥滈
作者: zaqmlp    时间: 2021-4-28 19:50

回复 3# chounan


打包几个文件用网盘分享出来
作者: chounan    时间: 2021-4-28 20:18

回复 4# zaqmlp


上q传你吧




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2