本帖最后由 zaqmlp 于 2020-9-11 22:18 编辑
回复 3# lxh623 - <# :
- cls
- @echo off
- cd /d "%~dp0"
- powershell -NoProfile -ExecutionPolicy bypass "Invoke-Command -ScriptBlock ([ScriptBlock]::Create([IO.File]::ReadAllText('%~f0',[Text.Encoding]::Default))) -Args '%~dp0'"
- pause
- exit
- #>
- $path=$args[0];
- $outfile=$path+'#result.csv';
- $enc=[Text.Encoding]::UTF8;
- $fs=New-Object System.IO.FileStream($outfile, [System.IO.FileMode]::Create);
- $sw=New-Object System.IO.StreamWriter($fs, $enc);
- $files=@(dir -liter $path -recurse|?{('.xml' -eq $_.Extension) -and ($_ -is [System.IO.FileInfo])});
- for($i=0;$i -lt $files.length;$i++){
- write-host $files[$i].FullName;
- $text=[IO.File]::ReadAllText($files[$i].FullName, $enc);
- $m1=[regex]::matches($text, '<char xml:id="([^"]*?)">([\s\S]+?)</char>');
- if($m1.count -ge 1){
- foreach($k in $m1){
- $arr=@($files[$i].BaseName,'','','','','','','','','');
-
- $a=$k.groups[1].value;
- $arr[1]=$a;
-
- $b=[regex]::match($k.groups[2].value,'[^>]+(?=</charName>)');
- if($b.success){$arr[2]=$b.groups[0].value;};
-
- $m2=[regex]::matches($k.groups[2].value, '(?<=<value>).+?(?=</value>)');
- $n=3;
- if($m2.count -ge 1){
- foreach($v in $m2){
- $arr[$n]=$v.groups[0].value;
- $n++;
- };
- };
-
- $f=[regex]::match($k.groups[2].value,'<mapping type="[^"]*?unicode">([\s\S]+?)</mapping>');
- if($f.success){$arr[6]=$f.groups[1].value;};
-
- $g=[regex]::match($k.groups[2].value,'type="PUA">([\s\S]+?)</mapping>');
- if($g.success){$arr[7]=$g.groups[1].value;};
-
- $h=[regex]::match($k.groups[2].value,'cb:dec="([^"]+?)".+?type="PUA"');
- if($h.success){$arr[8]=$h.groups[1].value;};
-
- $reg='<g ref="#'+$a+'">(.+?)</g>'
- $p=[regex]::match($text, $reg);
- if($p.success){$arr[9]=$p.groups[1].value;};
-
- $line=$arr -join ',';
- $sw.WriteLine($line);
- $sw.Flush();
- };
- };
- };
- $sw.Close();
- $fs.Close();
复制代码
|