Compressing multiple Files

Hello :slight_smile:

I’m trying to compress multiple files via powershell. The script works fine, as long every file exists in the source folder, but runs on an error if one misses. Is there a way, to supress the error and go on with the next file?

Also if someone is kind enouth:
How could i write the script in a compacter way? I am still at my early scripting stage, so every help is welcome^^

Thanks in advance! :slight_smile:

$source = "C:\Program Files (x86)\IBM\Notes"
$target = "Y:\Backup"

$compress = @{
  Path = "$source\*.id", "$source\notes.ini", "$source\name.nsf", "$source\desktop.ndk"
  CompressionLevel = "Fastest"
  DestinationPath = "$target\Notes-Sicherung"
}

if ((Test-Path -Path $target) -eq $false){
    New-Item -Path "Y:\" -Name "Backup" -ItemType Directory
}

if (((Test-Path -Path $source) -eq $true) -and ((Test-Path -Path $target) -eq $true)){
    Compress-Archive @compress -Update
}

You may read about the parameter -ErrorAction

“Compact” does not necessarily mean better. Actually it is a best practice to write your code as verbose as possible. If another person reads your code it is much easier to understand and to maintain if the code is verbose. And in 3 or 6 month you are another person as well. :wink:

One minor little issue I see in your code though. :wink:

You are checking if the target folder exists and if not you create it. Then in your next if condition you check the target folder again. That’s redundant. :wink:

And because of Test-Path already returns a boolean you don’t need to compare it against $true or $false. So it will be enough when you write

if(Test-Path -Path $target)

Olaf, your just great man, or shoud I say “Genialer Typ” :slight_smile:

I try to write my scripts simple and verbose at first and try to make them compact afterwards, just to learn a little bit more, but youre right. At least my colleages may have trouble understand the scripts then :wink:

Good advice with the $true statements, i corrected it, thank you :slight_smile:

But:
I have problems understanding how to apply the -ErrorAction parameter with multiple paths at once.
If i only wirte Compress-Archive @compress -Update -ErrorAction SilentlyContinue theres still the error.

I did it like this now, but wonder if its possible with all paths at once.

$source = "C:\Program Files (x86)\IBM\Notes"
$target = "Y:\Backup"

$ErrorActionPreference = "SilentlyContinue"
<#
$compress = @{
  Path = "$source\*.id", "$source\notes.ini", "$source\name.nsf", "$source\desktop.ndk"
  CompressionLevel = "Fastest"
  DestinationPath = "$target\Notes-Sicherung"
}
#>

if (-not (Test-Path -Path $target)){
    New-Item -Path "Y:\" -Name "Backup" -ItemType Directory
}

if (Test-Path -Path $source){
    #Compress-Archive @compress -Update -ErrorAction SilentlyContinue
    Compress-Archive -Path "$source\*.id" -DestinationPath "$target\Notes-Sicherung"
    Compress-Archive -Path "$source\notes.ini" -DestinationPath "$target\Notes-Sicherung"
    Compress-Archive -Path "$source\name.nsf" -DestinationPath "$target\Notes-Sicherung"
    Compress-Archive -Path "$source\desktop.ndk" -DestinationPath "$target\Notes-Sicherung"
}

PS: I also looked at Try Catch, but I’m not really sure if it would work.
I could try the paths (at once) first, but i am not sure, what to do in the catch statement.

If I got it right you want to provide a list of paths for Compress-Archive to be zipped together, right? So you may collect all valid paths in advance and provide this - already checked - list to your compress command. … so it should look something like this:

$source = "C:\Program Files (x86)\IBM\Notes"
$target = "Y:\Backup"
$Path = "$source\*.id", "$source\notes.ini", "$source\name.nsf", "$source\desktop.ndk"


$ValidPaths = Get-Item -Path $Path -ErrorAction SilentlyContinue

if (-not (Test-Path -Path $target)) {
    New-Item -Path "Y:\" -Name "Backup" -ItemType Directory
}

$compress = @{
    Path             = $ValidPaths
    CompressionLevel = "Fastest"
    DestinationPath  = "$target\Notes-Sicherung"
}

if (Test-Path -Path $source) {
    Compress-Archive @compress -Update
}

Another tip: You may try to keep variable declarartions visually close to their use if possible. It is confusing when you have the splatting hashtable at the beginning of your code and then you have to search where you used it. I think it’s better to have them close to each other.

Edit:
Actually your check for the source path should be much earlier I think. It should be before you create the list of source files. :wink:

1 Like

Thats exactly what i was trying to do!
Very good idea to check the paths beforehand, didnt think of that…!

Yeah, the script organisation is another thing :smiley:

I corrected it, lik u said in your edit :slight_smile: I also added another if to check if the Y: drive is mapped at all. Should have thought of that earlyer ^^

Many thanks for your help!

I don’t like mapped network drives at all. I used to use UNC paths instead. :wink:

Funny enouth, i changed it 5 minutes ago :smiley: