本帖最后由 WHY 于 2019-6-17 15:14 编辑
- $file1 = '1.xml'; #修改前的 xml 文件
- $file2 = '2.xml'; #修改后的 xml 文件
- $path = 'E:\Test'; #存放图片的文件夹
-
- $str = [IO.File]::ReadAllText($file1, [Text.Encoding]::Default);
- $str = $str -replace '(?<!<[^<>\s]+)\s+' -replace '<!--(?:(?!-->).)*-->';
-
- $arr = $str -split '(?<=<[^<>]*>)'; #以元素节点为单元分割数组
- [Collections.ArrayList]$List = @();
-
- for ( $i=0; $i -lt $arr.Count; $i++ ){
- $m = $arr[$i] -cmatch '<Time\s|<ImageNumber\s|\bsrc=.*\bsrcid=|\bsrcid=.*\bsrc='; #增加过滤条件
- if ($m) {
- forEach ( $a In [regex]::Matches($arr[$i], '(?<=["/])[^"/]+(?=\.(?i:png|jpg|webp))') ){
- $key = $a.Value.ToLower() + '_*';
- if ( !$List.Contains($key) ){
- [void]$List.Add($key);
- }
- }
- } else {
- $arr[$i] = $arr[$i] -replace '\.(?:png|jpg|webp)';
- }
- }
-
- [IO.File]::WriteAllText($file2, $arr -join '', [Text.Encoding]::UTF8);
- dir $path -Include *.png, *.jpg, *.webp -Exclude $List -Recurse | ren -NewName{$_.BaseName}
-
- echo 'Done';
- [Console]::ReadKey();
复制代码
|