View difference between Paste ID: fK0WTtmh and mLizttME
SHOW: | | - or go back to the newest paste.
1
# bulkUnzipFiles.ps1
2
3
# This script will take a directory of zip files, unzip them to individual folders, and delete the source zip files
4
# Contains error handling and logging
5
# Will fail if source files contain square bracekts
6
7
$SourceFileLocation="E:\source\*"
8
$DestinationFileLocation="e:\destination\"
9
10
$ErrorLogfile=[System.IO.Path]::GetTempFileName()
11
12
$listOfFiles=get-childitem $SourceFileLocation -Include *.zip
13
14
Add-Type -AssemblyName System.IO.Compression.FileSystem
15
function Unzip
16
{
17
    param([string]$zipfile, [string]$outpath)
18
19
    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
20
}
21
22
23
Foreach ($file in $listOfFiles)
24
{
25
    
26
       
27
    $UnzipErrorMessage=$NULL
28
    $DeleteErrorMessage=$NULL
29
        
30
    $DestinationFolder=$DestinationFileLocation+$File.Basename
31
    
32
    write-host $file.fullname  ----->  $DestinationFolder
33
    try
34
    {
35
        Unzip $file.fullname $DestinationFolder -erroraction stop
36
    }
37
    Catch
38
    {
39
         $UnzipErrorMessage=$_.Exception.Message
40
         Write-host $UnzipErrorMessage+"`r`n" | Out-File -FilePath $ErrorLogfile -Append
41
         $file.fullname+" threw "+$UnzipErrorMessage | Out-File -FilePath $ErrorLogfile -Append
42
         
43
    }
44
    if (-Not $UnzipErrorMessage)
45
    {
46
        try
47
        {
48
            write-host Deleting $file.fullname
49
            # Remove-item $file.fullname  -erroraction stop -whatif
50
            Remove-item $file.fullname -erroraction stop
51
        }
52
        Catch
53
        {
54
            $DeleteErrorMessage=$_.Exception.Message
55
            Write-Host $DeleteErrorMessage
56
            $file.fullname+" threw "+$DeleteErrorMessage | Out-File -FilePath $ErrorLogfile -Append
57
        }
58
    }
59
60
}
61
62
$ErrorLogfile=Get-ChildItem $ErrorLogfile
63
If ($ErrorLogFile.length -ne 0)
64
{
65
    write-error "Errors were encountered.  Error log is here: $ErrorLogFile`r`n"
66
    get-content $ErrorLogfile
67
}
68
else
69
{    
70
    remove-item -path $ErrorLogfile
71
}
72
73
# references
74
# https://stackoverflow.com/questions/27768303/how-to-unzip-a-file-in-powershell
75
# http://anonit.net/create-a-zip-file-on-powershell/