0

So I got the code to work how I like it for individual files. Based on some of the suggestions below, I was able to come up with this:

$Path = "C:\Users\User\Documents\PowerShell\"

$Num = 160
$ZipFile = "FileGroup0000000$Num.zip"
$File = "*$Num*.txt"
$n = dir -Path $Path$File | Measure

        if($n.count -gt 0){
        Remove-Item $Path$ZipFile
        Compress-Archive -Path $Path$File -DestinationPath $Path
        Rename-Item $Path'.zip' $Path'FileGroup0000000'$Num'.zip'          
        Remove-Item $Path$File            

        }    
        else {
            Write-Output "No Files to Move for FileGroup$File"
        }

The only thing I need to do now is have $Num increment after the program finishes each time. Therefore the program will run, and then move $Num to 160, 161, etc. and I will not have to re initiate the code manually. Thanks for the help so far.

  • this `$Number = "$Num"` converts your `$Num` numeric string into _another numerc string_. [*grin*] just use the number, not a string. to get leading zeros, use the string format operator `-f` and the way that it allows converting _numbers_ to strings ... with leading `0` characters if you tell it to do so. – Lee_Dailey Mar 07 '20 at 23:27
  • @somebadhat this is just how I'm testing my script on my personal computer. I want to use this script to zip EDI files into a respective folder on one of my company's production servers – cfdeangelis Mar 09 '20 at 18:52
  • @somebadhat I apologize for the confusion. I'm a bit new here. Anyways, I have updated my code and narrowed down the problem to one constraint, looping the $Num – cfdeangelis Mar 11 '20 at 23:27
  • your extraction code is not in the question. – somebadhat Mar 12 '20 at 01:00
  • your code would be easier to read if you replaced `$Path = "C:\Users\User\Documents\PowerShell\"` with `pushd "C:\Users\User\Documents\PowerShell\"` and remove `$Path`. – somebadhat Mar 12 '20 at 01:06
  • how does your script find `FileGroup0000000159.zip`? – somebadhat Mar 12 '20 at 01:10
  • Why 1. expand archive to folder 2. move files to folder 3. compress folder 4. delete source 5. rename archive 6. delete folder when you can just add files to the archive? – somebadhat Mar 12 '20 at 05:56
  • `1. expand archive to folder` How do you find the archive? Where is the archive? Where is the folder? `2. move files to folder` Move what files from where? `3. compress folder` `4. delete archive` `5. rename archive` `6. delete folder` – somebadhat Mar 12 '20 at 13:25
  • Why `$n = dir -Path $Path$File | Measure` when you could just not open any `FileGroup0000000000.zip` whose size is zero.? – somebadhat Mar 12 '20 at 13:30
  • see https://stackoverflow.com/questions/60664908/update-zipfiles-with-powershell-a-faster-way – somebadhat Mar 15 '20 at 23:28

1 Answers1

0

Your filename formatting should go inside the loop and you should use the Format operator -f to get the preceding zeros, like:

159..1250 | ForEach-Object {
    $UnzippedFile = 'FileGroup{0:0000000000}' -f $_
    $ZipFile = "$UnzippedFile.zip"
    Write-Host "Unzipping: $ZipFile"
    # Do your thing here
}
iRon
  • 20,463
  • 10
  • 53
  • 79
  • Do you know how I could get this to incorporate the files as part of the loop as well. The workflow first looks for 'FileGroup0000000159.zip', extracts it into a folder, and then looks for the files in the same directory as the original zip. The files look like '159.txt', '159.1.txt',etc. and then for 'FileGroup0000000160.zip', the files look like '160.txt', 160.1.txt', etc. The loop you suggested does work to move through the file groups but it does not iterate through the files themselves, placing them in their respective zip file. – cfdeangelis Mar 09 '20 at 19:55