这样行不?
保存ansi或gbk编码的bat ,然后放到包含需要处理的文件夹下运行
7z.exe所在路径和输出路径自行设置- @{}#? 2>nul&powershell -c "type -literalpath '%~f0'|out-string|iex"&exit/b
-
- $nanaz='Z:\Program Files\7-Zip\7z.exe'
- $outpath='Z:\Users\foobar\Desktop'
-
- $p=new-object System.Diagnostics.Process
- $p.StartInfo.UseShellExecute=$false
- $p.StartInfo.RedirectStandardOutput=$true
- $p.StartInfo.FileName=$nanaz
- $pargs='a -tzip {0} -so (-_-) "{1}"'
- $bf=new-object 'byte[]' (4*1024*1024)
-
- $files=gci -file
- foreach($file in $files){
- $dir=$file.basename
- if([io.directory]::exists($pwd.path+"\$dir")){
- $coding='default'
- $str=gc -LiteralPath ($file.fullname) -raw
- if($str -Match 'encoding *= *"utf-?8"'){
- $coding='utf8';$str=gc -LiteralPath ($file.fullname) -raw -encoding $coding
- }
- $pw="";if($str -match '(?<=uniquecode=")[^"]*(?=")'){$pw='-p'+$matches[0]}
- $p.StartInfo.Arguments=$pargs -f $pw,($dir+"\*")
- $p.start()
- $ms=new-object 'system.io.memorystream'
- $done=$false;while(-not $done){
- $c=$p.StandardOutput.BaseStream.read($bf,0,$bf.length)
- if($c){$ms.write($bf,0,$c)}else{$done=$true}
- }
- if($str -match '<..*? value="'){
- $v=$matches[0]
- $t=$str -split '<..*? value="',2
- }else{
- write-host "未找到value ,自动添加" -ForegroundColor yellow
- $v='<qwertyuiop Value="'
- $t=@($str,'" />')
- }
- $p.WaitForExit()
- $ms.capacity=$ms.length
- write-host ("保存到文件:"+$outpath+"\已合并_"+$file.name) -ForegroundColor green
- set-content -LiteralPath ($outpath+"\已合并_"+$file.name) -encoding $coding -value @(
- $t[0],$v,[convert]::ToBase64String($ms.getbuffer(),0,$ms.length),$t[1]) -nonewline
- }else{
- write-host "找不到文件夹: $dir" -ForegroundColor red
- }}
- "`ndone"
- cmd /c pause
复制代码
|