-2

I'm using a script from site link. But it zips the directory. I rather want it to zip only files inside that directory and the zipfile name should be the directory.zip.

Here is the ZipFolder function. Can someone tell me what needs to be changed in order to zip only files inside a directory?

function ZipFolder(
    [IO.DirectoryInfo] $directory)
{
    If ($directory -eq $null)
    {
        Throw "Value cannot be null: directory"
    }

    Write-Host ("Creating zip file for folder (" + $directory.FullName + ")...")

    [IO.DirectoryInfo] $parentDir = $directory.Parent

    [string] $zipFileName

    If ($parentDir.FullName.EndsWith("\") -eq $true)
    {
        # e.g. $parentDir = "C:\"
        $zipFileName = $parentDir.FullName + $directory.Name + ".zip"
    }
    Else
    {
        $zipFileName = $parentDir.FullName + "\" + $directory.Name + ".zip"
    }

    If (Test-Path $zipFileName)
    {
        Throw "Zip file already exists ($zipFileName)."
    }

    Set-Content $zipFileName ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))

    $shellApp = New-Object -ComObject Shell.Application
    $zipFile = $shellApp.NameSpace($zipFileName)

    If ($zipFile -eq $null)
    {
        Throw "Failed to get zip file object."
    }

    [int] $expectedCount = (Get-ChildItem $directory -Force -Recurse).Count
    $expectedCount += 1 # account for the top-level folder

    $zipFile.CopyHere($directory.FullName)

    # wait for CopyHere operation to complete
    WaitForZipOperationToFinish $zipFile $expectedCount

    Write-Host -Fore Green ("Successfully created zip file for folder (" `
        + $directory.FullName + ").")
}

#Remove-Item "C:\NotBackedUp\Fabrikam.zip"

[IO.DirectoryInfo] $directory = Get-Item "D:\tmp"
ZipFolder $directory
user
  • 19
  • 1
  • 4
  • First of all, welcome! I did not notice, but you are on serverfault, here, and you should have been asking this on stackoverflow.com. You can be forgiven, but not me, I should not have answered this and directed you to https://stackoverflow.com/ instead. – thecarpy Nov 15 '18 at 11:50

2 Answers2

2

First of all, welcome.

If you read the code, you can see that the folder is copied, here, into the zip file:

$zipFile.CopyHere($directory.FullName)

What you would need to do instead is copy the individual files in the directory.

Basically, you need to cd into the directory, and copy each childItem.

cd $Directory.FullName
foreach ($f in (get-childItem .))
{
  $zipFile.CopyHere($f)
}

Note that you will also have to remove:

$expectedCount += 1 # account for the top-level folder

Try that, note that I have note tested/debugged it at all, this is just pseudo code, how I would do it. Powershell is nice and powerful, but the learning curve is steep.

thecarpy
  • 176
  • 5
0

Found an alternate method from serverfault

$srcdir = "H:\Backup"
$zipFilename = "test.zip"
$zipFilepath = "K:\"
$zipFile = "$zipFilepath$zipFilename"

#Prepare zip file
if(-not (test-path($zipFile))) {
    set-content $zipFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipFile).IsReadOnly = $false  
}

$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipFile)
$files = Get-ChildItem -Path $srcdir | where{! $_.PSIsContainer}

foreach($file in $files) { 
    $zipPackage.CopyHere($file.FullName)
#using this method, sometimes files can be 'skipped'
#this 'while' loop checks each file is added before moving to the next
    while($zipPackage.Items().Item($file.name) -eq $null){
        Start-sleep -seconds 1
    }
}
user
  • 19
  • 1
  • 4