[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
本帖最后由 WHY 于 2021-10-10 21:55 编辑

Test.vbs
  1. Rem On Error Resume Next
  2. Dim srcDir, dstDir
  3. srcDir = "E:\Test\新建文件夹"           '源文件夹路径,需替换为实际路径
  4. dstDir = "E:\Test\新建文件夹1"          '目标文件夹路径
  5. Dim wsc
  6. Set wsc = CreateObject("WScript.Shell")
  7. If WSH.Arguments.Count = 0 Then
  8.     wsc.Run "cscript " & chr(34) & WSH.ScriptFullName & chr(34) & " ARG", 0
  9.     WScript.Quit
  10. End If
  11. Dim dic1, fso
  12. Set dic1 = CreateObject("Scripting.Dictionary")
  13. Set fso  = CreateObject("Scripting.FileSystemObject")
  14. Dim reg
  15. Set reg     = New RegExp
  16. reg.Pattern = "[ \t]+"
  17. reg.Global  = True
  18. Dim objExec
  19. If Not fso.FolderExists(srcDir) Then MsgBox "源文件夹不存在" : WSH.Quit
  20. wsc.Run "cmd /c md " & chr(34) & dstDir & chr(34) & " 2>nul", 0              '创建目标文件夹
  21. Set objExec = wsc.Exec( "cmd /c dir /b /a-d /s " & chr(34) & srcDir & "\*.txt" & chr(34) )  '遍历源文件夹
  22. Rem 字典dic1,key=文件名,value=文件路径
  23. Dim strFilePath, strFileName
  24. Do Until objExec.StdOut.AtEndOfStream
  25.     strFilePath = objExec.StdOut.ReadLine
  26.     strFileName = fso.GetFile(strFilePath).Name
  27.     strFileName = LCase(strFileName)
  28.     If Not dic1.Exists(strFileName) Then
  29.         dic1.Add strFileName, strFilePath
  30.     Else
  31.         dic1.Item(strFileName) = dic1.Item(strFileName) & vbLf & strFilePath
  32.     End If
  33. Loop
  34. Rem 遍历字典dic1
  35. Dim key
  36. For Each key In dic1.Keys
  37.     GetFileContent key, Split( Dic1.Item(key), vbLf )
  38. Next
  39. Function GetFileContent(fileName, ByRef arrFilePath)
  40.     Dim dic2, i, objFile, strLine
  41.     Set dic2 = CreateObject("Scripting.Dictionary")
  42.     For i = 0 To UBound(arrFilePath)
  43.         Set objFile = fso.OpenTextFile(arrFilePath(i), 1)
  44.         Do Until objFile.AtEndOfStream
  45.             strLine = objFile.ReadLine
  46.             strLine = reg.Replace(strLine, "|")                                       '空格替换为"|"
  47.             If UBound(Split(strLine, "|")) >= 2 And InStr(strLine, "-QQ") <= 0 Then   '如果大于等于3列、不含 "-QQ"
  48.                 strLine = Replace(Replace(strLine,"SZ", "0|"),"SH", "1|")             '替换 "SZ" 和 "SH"
  49.                 If Not dic2.Exists(strLine) Then                                      '字典dic2,用于去重复
  50.                     dic2.Add strLine, True
  51.                 End If
  52.             End If
  53.         Loop
  54.         objFile.Close
  55.         Set objFile = Nothing
  56.     Next
  57.     fso.OpenTextFile(dstDir & "\" & fileName, 2, True).Write( Join(dic2.Keys, vbCrLf) )    '保存
  58.     Set dic2 = Nothing
  59. End Function
  60. MsgBox "Done"
复制代码
Test.PS1
  1. $dt = get-Date;
  2. $srcDir = 'E:\Test\新建文件夹';
  3. $dstDir = 'E:\Test\新建文件夹1';
  4. If (![IO.Directory]::Exists($dstDir)){ $null = md $dstDir }
  5. $dic = New-Object 'System.Collections.Generic.Dictionary[string, [Collections.ArrayList]]';
  6. forEach( $file In (dir $srcDir -Recurse -Filter '*.txt') ){
  7.     $Name = $file.BaseName.ToUpper();
  8.     $Path = $file.FullName;
  9.     if( !$dic.ContainsKey($Name) ){
  10.         $dic.Add($Name, @($Path));
  11.     } else {
  12.         [void]$dic[$Name].Add($Path);
  13.     }
  14. }
  15. forEach( $key In $dic.Keys ){
  16.     $Hash = @{};
  17.     for( $i=0; $i -lt $dic[$key].Count; $i++ ){
  18.         $arr = [IO.File]::ReadAllLines($dic[$key][$i], [Text.Encoding]::Default);
  19.         $arr = $arr -match '^(?:(?!-QQ)\S)+\s+\S+\s+\S+';
  20.         $arr = $arr -replace '\s+', '|' -replace 'SZ', '0|' -replace 'SH', '1|';
  21.         $Count = $arr.Count;
  22.         for( $j=0; $j -lt $Count; $j++ ){
  23.             if( !$Hash.ContainsKey($arr[$j]) ){
  24.                 $Hash.Add($arr[$j], $True);
  25.             }
  26.         }
  27.     }
  28.     [IO.File]::WriteAllLines( $dstDir + '\' + $key + '.txt', $Hash.Keys );
  29. }
  30. ((get-Date) - $dt).TotalSeconds;
复制代码

TOP

本帖最后由 WHY 于 2021-10-10 21:59 编辑

回复 59# xczxczxcz


    过奖,
感谢测试。
在我的电脑上,vbs 用时 3.5 分钟,PowerShell 脚本用时 2.1 分钟

TOP

返回列表