How can I zip files in nested folders using PowerShell?
Asked by
radius.12
I'm trying to zip multiple folders with several layers of subfolders that contain .jpg and .png files. My goal is to create a zip file for each folder, using that folder's name for the zip file. For example, if I have a folder structure like this: 'Photos > Family > Brother', I want to create a file named 'brother.zip' that contains everything from the Brother folder. However, I have a PowerShell script that's supposed to do this, but it's not functioning as expected. I'd like some help troubleshooting it and getting the desired outcome.
Answers
It looks like the issue might be with how you're building the path for your zip files. Using `Join-Path $RootDirectory ($FolderName + ".zip")` could lead to problems if the structure is nested and you just need the current folder name. Instead, try creating the zip file in the same directory as the images like this: `$ZipFilePath = Join-Path $FolderPath ($FolderName + ".zip")` to ensure it's created right there in each folder. Also, make sure your script isn't accidentally creating duplicates by checking if thereโs already a zip file before creating a new one.
Reply to this answer
When zipping your files, itโs often helpful to first run a discovery process in your script to gather all file and folder details. Look at the files, report back what you find, then perform your compression based on that data. This method helps to ensure you know exactly what youโre dealing with before making any changes.
Reply to this answer
You might complicate things less by first gathering a list of all directories with image files. You could use `-File -Recurse` to collect these paths, then iterate through that list to create your zips. Just remember that directory names can conflict if you have similar folder names in different paths! Ensure to check the full path so there's no confusion.
Reply to this answer
Just to confirm, your plan is to create zip files alongside your images with the name of the parent folder, right? For example, if your images are in 'c:famevent', you'll have an 'event.zip' there containing all images from that directory? If so, you're on the right track! Just adjust where you're placing the zip files.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions