简体   繁体   中英

powershell script to archive files in many subfolders

i am trying to write down a script which can help me to archive multiple files in multiple subdirs. Also i need to exclude specific files. So far i got that few lines script

$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
  Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
  C:\Program Files\7-zip\7z.exe" a -t7z -mx=9 -ms=on $file
}

Basically its searches recursively all subfolders for .jpeg files and gives me the list of them excluding the ones that ends with 'i'.jpeg lets say 'photoi.jpeg'. This is working, but i cannot make it to the next step as i need to run 7zip for all listed files.

Can someone help me out here. Thanks in advance :)

Not sure if you are trying save one big zip lots of individual ones, however I do something like this:

Set-Alias sz "C:\Program Files\7-zip\7z.exe"

$files = Get-ChildItem -Recurse -path "D:\path\to\folder" -Exclude *i.jpeg |
  Where-Object { $_.FullName -notmatch '\\excludedir($|\\)' }
foreach ($file in $files)
{
  $output = sz a -t7z  -mx=9 -ms=on "$File" 2>&1
}

You might have to modify the zipping line has I have tested it using your command line options. Another nice touch is that I have captured the output of the command for reporting purposes.

You need to pass filenames, not objects, to 7z command.
@listfile syntax can help:

Get-ChildItem ...| Where-Object ...| select -expand fullname| out-file alist.txt -encoding utf8
'C:\Program Files\7-zip\7z.exe' a archive.7z -mx=9 -ms=on `@alist.txt
Remove-item .\alist.txt

note the backtick before @

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