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.

10
  • 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. Commented Mar 7, 2020 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 Commented Mar 9, 2020 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 Commented Mar 11, 2020 at 23:27
  • your extraction code is not in the question. Commented Mar 12, 2020 at 1: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. Commented Mar 12, 2020 at 1:06

1 Answer 1

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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.