试图从 zip powershell 中仅提取 .sql 文件 [英] Trying to extract just .sql files from zip powershell
问题描述
我正在尝试通过 zip 文件进行搜索,然后将所有 .sql 文件解压缩到一个目录中.我可以让它提取所有文件,但 zip 中有 200 多个 misc 文件,而我只需要 6 个 .sql.有没有一种简单的方法来指定 .sql?
I am trying to search thru a zip file and just extract all of the .sql files to a directory. I can make it extract all the files, but there are over 200 misc files in the zip, and I only need the 6 .sql's. Is there an easy way to designate just the .sql?
这是我试图开始工作的示例代码,如果有更好的方法,我很乐意听到.
Here is the example code that I was trying to get to work, if there is a better way, I would love to hear.
$shell = new-object -com shell.application
$zip = $shell.NameSpace("C:\Temp")
foreach($item in $zip.items()){
if([System.IO.Path]::GetExtension($item.Path) -eq ".sql"){
$shell.Namespace("C:\Project\").copyhere($item)
}
}
推荐答案
如果您拥有(或获取)PowerShell 社区扩展,你可以使用它的归档命令:
If you have (or grab) the PowerShell Community Extensions, you can use its archive commands:
Read-Archive C:\temp\foo.zip | %{$_} | Where Name -match '\.sql' | Expand-Archive
如果您在安装了 .NET 4.5 的系统上使用 PowerShell V3,您可以使用 System.IO.Compression.ZipFile
类来提取 sql 文件.
If you are on PowerShell V3 on a system with .NET 4.5 installed, you can use the System.IO.Compression.ZipFile
class to extract the sql files.
Add-Type -Assembly system.io.compression.filesystem
[IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
这篇关于试图从 zip powershell 中仅提取 .sql 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!