Search code examples
powershellarchive7zip

Create 7-Zip Archive with Files from Textfile - Powershell


I'm coding a small Powershell application at work for our supporters to Archive Files. But now im stuck because of the 7zip Option.

The Users can select a Day and Directory and the Code selects all Files which were modified before that Day.

Now i want to store all these Files in a Zip Archive but i don't get it :(

    $path = Read-Host "please select path : "

    Set-Alias sz "C:\Program Files\7-Zip\7zG.exe"


    $bis = Read-Host "please select date (dd.mm.yyyy): "
    $bisdate = [datetime]::ParseExact($bis,'dd.MM.yyyy',$null)

    $files = Get-ChildItem $path | Where {$_.LastWriteTime -lt $bisdate}  |     ForEach-Object { $_.FullName} > C:\Test\list.txt 

I found several samples for 7zip operations but can't find a solution for this Situation. Is there any option like in Bash? Something like that?

    zip archive -@ < out.txt

Solution

  • You can create archive with this code ifyou has 7z.exe and 7z.dll in specified directory:

    foreach ($filePath in [System.IO.File]::ReadAllLines("c:\Test\list.txt"))
    {
        $archParams += "`"" + $filePath + "`" "
    }
    $archParams = "a `"C:\Test\list.zip`" " + $archParams
    
    Start-Process -FilePath "C:\Program Files\7-Zip\7z.exe" -Wait -ArgumentList $archParams
    

    The escaping of qoutes are necessary because of space-symbol in paths to files