1

Im trying to create a script that when received the specific file it unzips it looks through the unzipped file and deletes the entire contents except for two or three specific folders with their contents. Then rezips it. Im at the point where the zipping, unzipping, and deleting works but when the delete is executed it looks through everything, not just the top layer of folders. Any suggestions? Heres my script so far:

$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\" 

2 Answers 2

0

If you only want to remove items in the top folder, you should remove that -recurse option from the Remove-Item command.

https://technet.microsoft.com/en-us/library/hh849765.aspx

0

Also, a lot of times you can use the -include or -exclude switches with commands which might help you say just get these files or get all , but these specific files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .