我试图创build一个脚本,当收到特定的文件解压缩它看起来通过解压缩的文件,并删除整个内容,除了两个或三个特定的文件夹及其内容。 然后重新解压缩。 我在压缩,解压缩和删除工作的地方,但是当执行删除时,它会查看所有内容,而不仅仅是顶层的文件夹。 有什么build议么? 我的脚本到目前为止:
$filesToZip="c:\temp\powertest\*" $zipfilename = "c:\temp\ziptest.zip" $originalzipfile = "c:\temp\powertest.zip" $extractionlocation = "c:\temp\" function Expand-ZIPFile($file, $destination) { $shell = new-object -com shell.application $zip = $shell.NameSpace($file) foreach($item in $zip.items()) { $shell.Namespace($destination).copyhere($item) } } Expand-ZIPFile –File $originalzipfile –Destination $extractionlocation # replace "New folder" and "New Text Document.txt" with folders and documents you dont want deleted specifiying the file extensions seperated by commas Remove-Item -recurse $filesToZip -exclude 'New folder' , 'New Text Document.txt' $value = $null if(-not (test-path($zipfilename))) { set-content $zipfilename $value #("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) (dir $zipfilename).IsReadOnly = $false } $shellApplication = new-object -com shell.application $zipPackage = $shellApplication.NameSpace($zipfilename) $files = dir $filesToZip foreach($file in $files) { $zipPackage.CopyHere($file.FullName) Start-sleep -milliseconds 500 } remove-item -recurse "C:\temp\powertest\"
如果您只想删除顶层文件夹中的项目,则应从“ Remove-Item命令中Remove-Item -recurse选项。
此外,很多时候你可以使用-include或-exclude开关,这些开关可能帮助你说只是获取这些文件或获取所有文件,但是这些特定的文件。