首先在电脑上找到winrar,或者unrar所在地,我的win7 x64下 :
winrar 在 'C:\Program Files (x86)\WinRAR\WinRAR.exe' unrar 在 'c:/Program Files (x86)/winrar/unrar.exe'
zip.ps1
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 cls; 2 $P = 'C:\Users\Patrick\Desktop\war\' 3 $winrar = 'C:\Program Files (x86)\WinRAR\WinRAR.exe' 4 Get-ChildItem -Path $p -Directory|%{ 5 &$winrar a "$($_).rar" $_ -y -ibck 6 Start-Sleep -Milliseconds 200 7 }
unzip.ps1
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 cls; 2 $P = 'C:\Users\Patrick\Desktop\war\archive\' 3 $winrar = 'C:\Program Files (x86)\WinRAR\WinRAR.exe' 4 Get-ChildItem -Path "$($p)*.rar" -File|%{ 5 &$winrar x "$_" "$P" -y -ibck 6 #Start-Sleep -Milliseconds 3000 7 }
unrar.ps1
![](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
1 cls; 2 $P = 'C:\Users\Patrick\Desktop\war\' 3 $unrar = 'c:/Program Files (x86)/winrar/unrar.exe' 4 cd $p 5 $rarfiles = @() 6 ls "$P*.rar"|%{ 7 &$unrar x $_.FullName $_.name.Substring(0,$_.name.IndexOf('.')) –y 8 }
例子:我的目录下有以下文件:
1 PS E:\AddOn\005batch script\DB5\1010\TSQL> ls 2 3 4 目录: E:\AddOn\005batch script\DB5\1010\TSQL 5 6 7 Mode LastWriteTime Length Name 8 ---- ------------- ------ ---- 9 d---- 2014/10/10 13:02 Archive 10 -a--- 2014/10/10 13:01 415 DUMMPREPVALIDATION.bat 11 -a--- 2014/10/10 13:02 524 DUMMPREPVALIDATION.ps1 12 -a--- 2014/10/7 16:10 2180 DUMMPREPVALIDATION.SQL 13 -a--- 2014/10/10 13:01 375 DUMMYREP.bat 14 -a--- 2014/10/10 13:02 484 DUMMYREP.ps1 15 -a--- 2014/10/7 16:09 1124 DUMMYREP.SQL 16 17 18 PS E:\AddOn\005batch script\DB5\1010\TSQL>
运行,把6个文件,按文件名为包名,打包到Archive目录下,打包成DUMMYREP.rar和DUMMPREPVALIDATION.rar,每个包中分别包含xxx.bat,xxx.ps1,xxx.sql三个文件:
1 2 cls 3 $pa = 'E:\AddOn\005batch script\DB5\1010\TSQL\' 4 $winrar = 'C:\Program Files (x86)\WinRAR\WinRAR.exe' 5 cd $pa 6 ls "$($pa)*.sql"|%{ 7 $f = $_.name.Substring(0,$_.name.IndexOf('.')) 8 ls "$($f).*"|%{ 9 #&$winrar a "$($pa)$($f).rar" $_ -y -ibck 10 &$winrar a "$($pa)Archive\$($f).rar" $_.Name -y -ibck 11 } 12 }