简体   繁体   中英

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.

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

采用

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM