Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # bulkUnzipFiles.ps1
- # This script will take a directory of zip files, unzip them to individual folders, and delete the source zip files
- # Contains error handling and logging
- # Will fail if source files contain square bracekts
- $SourceFileLocation="E:\source\*"
- $DestinationFileLocation="e:\destination\"
- $ErrorLogfile=[System.IO.Path]::GetTempFileName()
- $listOfFiles=get-childitem $SourceFileLocation -Include *.zip
- Add-Type -AssemblyName System.IO.Compression.FileSystem
- function Unzip
- {
- param([string]$zipfile, [string]$outpath)
- [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
- }
- Foreach ($file in $listOfFiles)
- {
- $UnzipErrorMessage=$NULL
- $DeleteErrorMessage=$NULL
- $DestinationFolder=$DestinationFileLocation+$File.Basename
- write-host $file.fullname -----> $DestinationFolder
- try
- {
- Unzip $file.fullname $DestinationFolder -erroraction stop
- }
- Catch
- {
- $UnzipErrorMessage=$_.Exception.Message
- Write-host $UnzipErrorMessage+"`r`n" | Out-File -FilePath $ErrorLogfile -Append
- $file.fullname+" threw "+$UnzipErrorMessage | Out-File -FilePath $ErrorLogfile -Append
- }
- if (-Not $UnzipErrorMessage)
- {
- try
- {
- write-host Deleting $file.fullname
- # Remove-item $file.fullname -erroraction stop -whatif
- Remove-item $file.fullname -erroraction stop
- }
- Catch
- {
- $DeleteErrorMessage=$_.Exception.Message
- Write-Host $DeleteErrorMessage
- $file.fullname+" threw "+$DeleteErrorMessage | Out-File -FilePath $ErrorLogfile -Append
- }
- }
- }
- $ErrorLogfile=Get-ChildItem $ErrorLogfile
- If ($ErrorLogFile.length -ne 0)
- {
- write-error "Errors were encountered. Error log is here: $ErrorLogFile`r`n"
- get-content $ErrorLogfile
- }
- else
- {
- remove-item -path $ErrorLogfile
- }
- # references
- # https://stackoverflow.com/questions/27768303/how-to-unzip-a-file-in-powershell
- # http://anonit.net/create-a-zip-file-on-powershell/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement