0

I am trying to zip all folders I find in my folder called services.

I use Get-Childitem to find these folders and I want to add the function after the pipeline, but it doesn't work out the way I want. The zip file should have the same name as the folder itself, so I tried to give the name with "$.FullName" and destinationpath is the folder "C:\com\$.Name"

Here is my script :

Get-ChildItem "C:\com\services" | % $_.FullName 


$folder = "C:\com\services"
$destinationFilePath = "C:\com"

function create-7zip([String] $folder, [String] $destinationFilePath)
{
    [string]$pathToZipExe = "C:\Program Files (x86)\7-Zip\7zG.exe";
    [Array]$arguments = "a", "-tzip", "$destinationFilePath", "$folder";
    & $pathToZipExe $arguments;
}
Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
RayofCommand
  • 4,054
  • 17
  • 56
  • 92

2 Answers2

1

First. Declare variables like folder and destination path.

Second. Change your 7zip folderpath as mine is in (Program Files).

 #declare variables
    $folder = "C:\com\services"
    $destPath = "C:\destinationfolder\"

    #Define the function
    function create-7zip{
    param([String] $folder, 
    [String] $destinationFilePath)
    write-host $folder $destinationFilePath
    [string]$pathToZipExe = "C:\Program Files\7-Zip\7z.exe";
    [Array]$arguments = "a", "-tzip", "$destinationFilePath", "$folder";
    & $pathToZipExe $arguments;
    }

     Get-ChildItem $folder | ? { $_.PSIsContainer} | % {
     write-host $_.BaseName $_.Name;
     $dest= [System.String]::Concat($destPath,$_.Name,".zip");
     (create-7zip $_.FullName $dest)
     } 

$_.PSIsContainer will find only the folders, constructing destination path variable $dest and then calling the function. I hope this helps.

Mitul
  • 9,734
  • 4
  • 43
  • 60
  • hey mitul, very helpful, the only thing is, that the zip files are stored up in the folder where my script is. but i will probably figure out :) – RayofCommand Aug 30 '13 at 09:33
1

If I understand you correctly, you want to pipe the output of gci into your Create-7Zip function, and have the function create a zip file named after each directory you're passing in, like this:

gci | ?{ $_.PSIsContainer } | Create-7Zip

To do this you'll need the cmdlet you're writing to support taking values from the pipeline, which you do with a [Parameter] attribute in your list of params().

function Create-7Zip
{
  param(
    [Parameter(ValueFromPipeline=$True)]
    [IO.DirectoryInfo]$Directory #we're accepting directories from the pipeline.  Based on the directory we'll get the zip name
    );
    BEGIN
    {
        $7Zip = Join-Path $env:ProgramFiles "7-Zip\7z.exe"; #get executable
    }
    PROCESS
    {
        $zipName = $("{0}.zip" -f $Directory.Name);
        $7zArgs = Write-Output "a" "-tzip" $zipName $directory.FullName; #Q&D way to get an array
        &$7Zip $7zArgs
    }
}



Usage:
    #Powershell 3.0
    get-childitem -directory | Create-7Zip
    #Powershell 2
    get-childitem | ?{ $_.PSIsContainer } | Create-7Zip

You'll see the output of 7zip; you can capture this information by piping it to somewhere else.

  • thanks, i am still very new to powershell. so for me it didnt work the way i wanted. why do you go for the executable the way u go? why don't simply give the fullpath ? thanks – RayofCommand Aug 30 '13 at 09:20
  • Full path would be fine, I just tend to try to replace any windows paths with their environmental variable equivalents, if possible. For program files the utility is less obvious, but for something like a temporary directory (generally c:\Users\\AppData\Local\Temp) using $env:Temp is safer for multiple users and easier to type. – David Beiler Aug 30 '13 at 13:28
  • yes of course, but especially 7zip has 2 different executables on some versions its 7z.exe and sometimes its 7zG.exe – RayofCommand Aug 30 '13 at 13:29
  • I _believe_ 7zG gives you some sort of GUI around the command line while providing the same functionality, but I haven't verified. – David Beiler Aug 30 '13 at 15:16