JohnnyP
JohnnyP

Reputation: 25

Powershell and 7zip - multiple files from multiple (sub)folders

I'm working on a (quite simple) powershell script that has to compress multiple files (.bak files in this case) from multiple subfolders to a destination folder.

Something like this:

From:

f:\backup\user1\file1.bak
f:\backup\user2\file2.bak

To:

x:\backupfolder\users\file1.zip
x:\backupfolder\users\file2.zip

Script:

$path = "f:\backup\"
$dest = "x:\backupfolder\users\"
$mask = "file*.bak"

$days = 1

$files = dir $path -Recurse -Include $mask | where {
    ($_.LastWriteTime -gt (Get-Date).AddDays(-$days).AddHours(-$hours).AddMinutes(-$mins)) -and ($_.psIsContainer -eq $false)
    }

ForEach ($file in $files) 
    {
    7z.exe a -tzip "$dest\$file.name.zip" $file
    }

The problem is that, in this case, i get The specified path is invalid error, because 7zip tries to create an archive in the form of this

x:\backupfolder\users\f:\backup\user1\file1.bak.name.zip

And if i try this:

7z.exe a -tzip "$dest\$_.basename.zip" $file

I get an archive x:\backupfolder\users\.basename.zip

All i want this script to do is compress files (file1.bak) to an archive with the same name (file1.zip) to a folder i told it to.

What am i doing wrong??

TYVM.

Upvotes: 0

Views: 6951

Answers (2)

ALIENQuake
ALIENQuake

Reputation: 282

Use

7z.exe a -tzip "$dest\$($file.basename).zip" $file

Upvotes: 1

notjustme
notjustme

Reputation: 2494

You're close. Try this 7z.exe a -tzip "$dest\$($file.basename).zip" $file

Upvotes: 2

Related Questions