本帖最后由 zaqmlp 于 2019-5-22 10:12 编辑
- @echo off
- cd /d "%~dp0"
- set "folder=结果"
- md "%folder%" 2>nul
- powershell -NoProfile -ExecutionPolicy bypass ^
- $files=@(dir^|?{($_ -is [System.IO.FileInfo]) -and ($_.Extension -eq '.xml')});^
- $arr='ABCDEFGHIJKLMNOPQRSTUVWXYZ'.toCharArray();^
- for($i=0;$i -lt $files.length;$i++){^
- write-host $files[$i].Name;^
- $dic=New-Object 'System.Collections.Generic.Dictionary[string,string]';^
- $list=New-Object 'System.Collections.Generic.Dictionary[string,string]';^
- $text=[IO.File]::ReadAllText($files[$i].FullName, [Text.Encoding]::UTF8);^
- $m=[regex]::matches($text,'name=\""([^^\""]+)\""');^
- $newtext=$text;^
- if($m.Count -ge 1){^
- for($j=0;$j -lt $m.Count;$j++){^
- $kw=$m[$j].groups[1].value;^
- if(@('=','%%') -notcontains $kw){^
- if(-not $dic.ContainsKey($kw)){^
- $c=@(get-random -input $arr -count 3) -join '';^
- while($list.ContainsKey($c)){$c=@(get-random -input $arr -count 3) -join '';};^
- $list.add($c,'');^
- $newtext=$newtext.replace($m[$j].groups[0].value,'name=\"'+$c+'\"');^
- $dic.add($kw,$c);^
- $n++;^
- };^
- };^
- };^
- $k=@($dic.keys^|sort {$_.length} -des);^
- $newtext=[regex]::replace($newtext,'(?^<==)\""[^^\""]+?\""',{param($a);$b=$a.value;foreach($it in $k){$b=$b.replace(('@'+$it),('@'+$dic[$it])).replace(('#'+$it),('#'+$dic[$it]));};$b});^
- [IO.File]::WriteAllText(('%folder%\'+$files[$i].Name), $newtext, [Text.Encoding]::UTF8);^
- };^
- }
- pause
复制代码
|