正在尝试从zip powershell中提取.sql文件
我试图通过一个zip文件进行搜索,然后将所有的.sql文件解压缩到一个目录中。我可以让它提取所有文件,但是zip中有200多个杂项文件,我只需要6.sql。是否有一种简单的方法来指定.sql 下面是我试图开始工作的示例代码,如果有更好的方法,我很乐意听到正在尝试从zip powershell中提取.sql文件,powershell,Powershell,我试图通过一个zip文件进行搜索,然后将所有的.sql文件解压缩到一个目录中。我可以让它提取所有文件,但是zip中有200多个杂项文件,我只需要6.sql。是否有一种简单的方法来指定.sql 下面是我试图开始工作的示例代码,如果有更好的方法,我很乐意听到 $shell = new-object -com shell.application $zip = $shell.NameSpace(“C:\Temp”) foreach($item in $zip.items()){ if([Sy
$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)
}
}
如果您有(或抓取),可以使用其存档命令:
Read-Archive C:\temp\foo.zip | %{$_} | Where Name -match '\.sql' | Expand-Archive
如果您在安装了.NET 4.5的系统上使用PowerShell V3,则可以使用system.IO.Compression.ZipFile
类来提取sql文件
Add-Type -Assembly system.io.compression.filesystem
[IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
如果您有(或抓取),可以使用其存档命令:
Read-Archive C:\temp\foo.zip | %{$_} | Where Name -match '\.sql' | Expand-Archive
如果您在安装了.NET 4.5的系统上使用PowerShell V3,则可以使用system.IO.Compression.ZipFile
类来提取sql文件
Add-Type -Assembly system.io.compression.filesystem
[IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
如果您有(或抓取),可以使用其存档命令:
Read-Archive C:\temp\foo.zip | %{$_} | Where Name -match '\.sql' | Expand-Archive
如果您在安装了.NET 4.5的系统上使用PowerShell V3,则可以使用system.IO.Compression.ZipFile
类来提取sql文件
Add-Type -Assembly system.io.compression.filesystem
[IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
如果您有(或抓取),可以使用其存档命令:
Read-Archive C:\temp\foo.zip | %{$_} | Where Name -match '\.sql' | Expand-Archive
如果您在安装了.NET 4.5的系统上使用PowerShell V3,则可以使用system.IO.Compression.ZipFile
类来提取sql文件
Add-Type -Assembly system.io.compression.filesystem
[IO.Compression.ZipFile]::ExtractToDirectory($zipPath, $extractPath)
我会稍微简化一下,使用变量而不是字符串文字,如下所示:
$shell = New-Object -COM 'Shell.Application'
$zipfile = 'C:\Temp\some.zip'
$destination = 'C:\Project'
$zip = $shell.NameSpace($zipfile)
$zip.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($destination).CopyHere($_)
}
但除此之外,您的代码应该做得很好
但是,请注意,它不会递归到zip文件中的嵌套文件夹中。处理嵌套文件夹时也需要类似的内容:
function ExtractZip($fldr, $dst) {
$fldr.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($dst).CopyHere($_)
}
$fldr.Items() | ? { $_.Type -eq 'File folder' } | % {
ExtractZip $_.GetFolder $dst
}
}
ExtractZip $shell.NameSpace($zipfile) $destination
我会稍微简化一下,使用变量而不是字符串文字,如下所示:
$shell = New-Object -COM 'Shell.Application'
$zipfile = 'C:\Temp\some.zip'
$destination = 'C:\Project'
$zip = $shell.NameSpace($zipfile)
$zip.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($destination).CopyHere($_)
}
但除此之外,您的代码应该做得很好
但是,请注意,它不会递归到zip文件中的嵌套文件夹中。处理嵌套文件夹时也需要类似的内容:
function ExtractZip($fldr, $dst) {
$fldr.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($dst).CopyHere($_)
}
$fldr.Items() | ? { $_.Type -eq 'File folder' } | % {
ExtractZip $_.GetFolder $dst
}
}
ExtractZip $shell.NameSpace($zipfile) $destination
我会稍微简化一下,使用变量而不是字符串文字,如下所示:
$shell = New-Object -COM 'Shell.Application'
$zipfile = 'C:\Temp\some.zip'
$destination = 'C:\Project'
$zip = $shell.NameSpace($zipfile)
$zip.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($destination).CopyHere($_)
}
但除此之外,您的代码应该做得很好
但是,请注意,它不会递归到zip文件中的嵌套文件夹中。处理嵌套文件夹时也需要类似的内容:
function ExtractZip($fldr, $dst) {
$fldr.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($dst).CopyHere($_)
}
$fldr.Items() | ? { $_.Type -eq 'File folder' } | % {
ExtractZip $_.GetFolder $dst
}
}
ExtractZip $shell.NameSpace($zipfile) $destination
我会稍微简化一下,使用变量而不是字符串文字,如下所示:
$shell = New-Object -COM 'Shell.Application'
$zipfile = 'C:\Temp\some.zip'
$destination = 'C:\Project'
$zip = $shell.NameSpace($zipfile)
$zip.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($destination).CopyHere($_)
}
但除此之外,您的代码应该做得很好
但是,请注意,它不会递归到zip文件中的嵌套文件夹中。处理嵌套文件夹时也需要类似的内容:
function ExtractZip($fldr, $dst) {
$fldr.Items() | ? { $_.Path -like '*.sql' } | % {
$shell.NameSpace($dst).CopyHere($_)
}
$fldr.Items() | ? { $_.Type -eq 'File folder' } | % {
ExtractZip $_.GetFolder $dst
}
}
ExtractZip $shell.NameSpace($zipfile) $destination