2

I'm trying to use PowerShell to compress a bunch of video files on my H:\ drive. However, running this serially would take a long time as the drive is quite large. Here is a short snippet of the code that I'm using. Some parts have been withheld.

$shows = Get-ChildItem H:\
foreach($show in $shows){
    Start-Job -ArgumentList $show -ScriptBlock {
        param($show)
        $destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
        Compress-Archive -Path $show.FullName -DestinationPath $destPath
    }
}

When I run a Get-Job, the job shows up as completed with no reason in the JobStateInfo, but no .zip was created. I've run some tests by replacing the Compress-Archive command with an Out-File of the $destPath variable using Start-Job as well.

Start-Job -ArgumentList $shows[0] -ScriptBlock {
    param($show)
    $show = [System.IO.FileInfo]$show
    $destPath = "$($show.DirectoryName)\$($show.BaseName).zip"
    $destPath | Out-File "$($show.DirectoryName)\test.txt"
}

A text file IS created and it shows the correct destination path. I've run PowerShell as an Administrator and tried again but that doesn't appear to work either. Not sure if it matters, but I'm running on Windows 10 (latest).

Any help would be appreciated. Thanks!

Henry Yam
  • 23
  • 3

2 Answers2

1

For some reason, inside the job, the serialized fileinfo object has no basename (it's a scriptproperty). If you have threadjobs, that works.

dir | start-job { $input } | receive-job -wait -auto | 
  select name,basename,fullname

Name      basename FullName
----      -------- --------
file1.txt          C:\Users\js\foo\file1.txt
file2.txt          C:\Users\js\foo\file2.txt
file3.txt          C:\Users\js\foo\file3.txt
js2010
  • 23,033
  • 6
  • 64
  • 66
  • 1
    So this actually revealed to me that I made some kind of mistake in my previous testing while trying to OutFile the basename to a .txt file as when I tried to replicate the test, I couldn't see the baseName anymore. What you pointed out was absolutely correct and switching over to using Start-ThreadJob after I ran "Install-Module threadjob" worked. Thanks a lot! – Henry Yam Mar 24 '20 at 19:35
0

Am not sure if that's what you want but I think you first need to create an archive then update that archive with shows so I created zip called archive and looped through adding files.

$shows = Get-ChildItem H:\


foreach($show in $shows){


        Compress-Archive -Path $show.FullName -Update -DestinationPath "C:\archive"
  }
3N16M4
  • 73
  • 10
  • Ah. Don't think that's what I'm looking for. I'm not trying to add to an existing archive. I'm trying to _create_ the archive. Each show/episode should be its own individual .zip file. I tried it out anyway just in case it would work and it didn't :(. Thanks though! – Henry Yam Mar 22 '20 at 23:37