It may be of use to extract .zip folders quickly and effectively, particular in large volumes. Rather than going through manually, one by one clicking extract here for each folder, waiting, then finding the next folder to unzip, do this via PowerShell instead. The scenario we have faced is archiving source files. Instead of having 15,000 files in a folder for March, they are contained in a .zip folder called 202403.zip – its much cleaner. However, what if your client requests a reload, what if you need to investigate the original source files for some old data? This is when we want to extract .zip folders at volume efficiently.

Optimize this process using PowerShell as follows:

  • Provide the file path containing the zipped folders
  • Extract files
  • Delete .zip folders

 

PowerShell script:

$FolderPath = "d:\folderroot\folder\" # Enter Folder to clean up

$files = Get-ChildItem $FolderPath -Filter '*.zip'
$files | ForEach-Object{ Expand-Archive -LiteralPath $_.FullName -DestinationPath $FolderPath}
$files | ForEach-Object{ Remove-Item -Path $_.FullName}

Edit the $FolderPath variable to the file path that holds your zipped folders and hit ‘Run’.

 

Want to extract to a different location? Differentiate folder paths between a source and destination, as below:

$ZippedFolderPath = "d:\folderroot\folder\" # Enter Folder path containing .zip files you want to clean up
$DestinationFolderPath = "d:\folderroot2\folder1\" # Enter Folder to extract files to

$files = Get-ChildItem $ZippedFolderPath -Filter '*.zip'
$files | ForEach-Object{ Expand-Archive -LiteralPath $_.FullName -DestinationPath $DestinationFolderPath}
$files | ForEach-Object{ Remove-Item -Path $_.FullName}

Hold on…you may wish to revert this…

You finish your investigation now your archive folder is a mess – this mass extraction needs to be undone. The following code allows you to zip a number of files based on the creation date. So those belonging to a single month are contained in the same .zip folder. Its cleaner and better for storage, a no brainer! You may wish to retain the most recent data outside a .zip folder for any data checks. In this example, we are cleaning up the archive folder and keeping everything within that, you can differentiate $sourceDirectory and $zipDirectory. The original CSVs in this case are deleted as they are stored in the .zip folders.

$sourceDirectory = "s:\folderroot\folder1\Archive" # Set the path to the directory containing the CSV / TXT files
$zipDirectory = "s:\folderroot\folder1\Archive" # Set the path where the zip files will be created

# Get all CSV files in the source directory
$csvFiles = Get-ChildItem -Path $sourceDirectory -Filter *.csv #change to .txt if needed

# Group CSV files by their creation year and month
$groupedFiles = $csvFiles | Group-Object { $_.CreationTime.ToString("yyyyMM") }

foreach ($group in $groupedFiles) {
$zipFileName = "$zipDirectory\$($group.Name).zip"

# Create the ZIP file and add the CSV files
Compress-Archive -Path $group.Group.FullName -DestinationPath $zipFileName -Force

# Delete the original CSV files after zipping
Remove-Item -Path $group.Group.FullName -Force
}
Write-Host "CSV files have been zipped and original files deleted."

The result:

It is short and simple but effective, it can save on storage and can run in the background.

Tags: