1

I am still pretty new to scripting and "programming" at all. if you miss any information here let me know. This is my working zip function:

 $folder = "C:\zipthis\"
 $destinationFilePath = "C:\_archive\zipped"

   function create-7zip{
    param([string] $folder, 
    [String] $destinationFilePath)
    write-host $folder $destinationFilePath
    [string]$pathToZipExe = "C:\Program Files (x86)\7-Zip\7zG.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)
     } 


create-7zip $folder $destinationFilePath

now I want him to zip special folders which I already sorted out :

get-childitem "C:\zipme\" | where-Object {$_.name -eq "www" -or $_.name -eq "sql" -or $_.name -eq "services"}

This small function finds the 3 folders I need called www, sql and services. But I didn't manage to insert this into my zip function, so that exactly this folders are zipped and put into C:\_archive\zipped

Because a string is used instead of an array, he tried always to look for a folder called wwwsqlservice which is not there. I tried to put an array using @(www,sql,services) but i had no success, so whats the right way, if there is one? It should compatible with powershell 2.0, no ps3.0 cmdlets or functions please.

thanks in advance!

4
  • Perhaps try @("www","sql","services")? I expect powershell would give you an error without making those array elements strings first. If this is helpful I can post a more fleshed out answer with an example. Commented Sep 19, 2013 at 13:06
  • that's what I did, changed the parameter to array and then he tries to make one name like wwwsqlservices and can't find it. Commented Sep 19, 2013 at 13:10
  • What is the proper syntax for passing multiple directories to 7zip for compression? Commented Sep 19, 2013 at 13:13
  • i guess i need something like % (foreach) and then let the function run 3 times, for every folder? Commented Sep 19, 2013 at 13:15

1 Answer 1

1

Here's a really simple example of what you want to do, removed from the context of your function. It assumes that your destination folders already exist (You can just use Test-Path and New-Item to create them if they don't), and that you're using 7z.exe.

$directories = @("www","sql","services")  
$archiveType = "-tzip"
foreach($dir in $directories)
{
    # Use $dir to update the destination each loop to prevent overwrites!
    $sourceFilePath = "mySourcePath\$dir"
    $destinationFilePath = "myTargetPath\$dir"

    cmd /c "$pathToZipExe a $archiveType $destinationFilePath $sourceFilePath"
}

Overall it looks like you got pretty close to a solution, with some minor changes needed to support the foreach loop. If you're confident that create-7zip works fine for a single folder, you can substitute that for the cmd /c line above. Here's a listing of some handy example usages for 7zip on the command line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.