none
Powershell copy some folders from a zip file RRS feed

  • Question

  • I have a zip folder it contains 10-12 folders (in folders i have some file) with different names and i need to get folders and copy into destination whose name contains 'abc' or 'xyz' like into one location and folder names contains 123 in one location using Power Shell Scripting.

    Thanks in advance.

    Tuesday, December 30, 2014 6:39 AM

Answers

  • ########################################################### # 
    COMMENT : Search zip files for specific files and extract # them to a (temp) directory. 
    ########################################################### 
    #ERROR REPORTING ALL Set-StrictMode -Version latest 
    #---------------------------------- 
    #STATIC VARIABLES 
    #---------------------------------- 
    $srcZipFilepath = “C:\Test\ZIPFILE.zip” 
    $tempPath = “C:\Temp” 
    $ABCDestination = “C:\Test123\ABC” 
    $XYZDestination = “C:\Test123\XYZ” 
    $PQRDestination = “C:\Test123\PQR” 
    #---------------------------------- 
    #FUNCTION CopyFilesToFolders 
    #---------------------------------- 
    Function CopyFilesToFolders { Param([string]$tempPath) #Write-Host $tempPath + "11" #Loop through each folder copy in destination $shell_app= New-Object -com shell.application $file = Get-ChildItem -Path $tempPath foreach($filist in $file) { #Write-Host $filist.Name + "21" if ($filist.Name -like "XYZ") {
    if (!(Test-Path -path $XYZDestination )) { New-Item $XYZDestination -type directory } #Write-Host $filist.fullname + "- XYZ -" + $XYZDestination Move-Item -Path $filist.fullname -Destination $XYZDestination } elseif($filist.Name -like "ABC") { if (!(Test-Path -path $ABCDestination)) { New-Item $ABCDestination -type directory } #Write-Host $filist.fullname + "- Services -" + $ABCDestination Move-Item -Path $filist.fullname -Destination $ABCDestination }
    else { if (!(Test-Path -path $PQRDestination)) { New-Item $PQRDestination -type directory } #Write-Host $filist.fullname + "- Application Location -" + $PQRDestination Move-Item -Path $filist.fullname -Destination $PQRDestination } } #Deleting the temp folder ReMove-Item -Path $tempPath }
    #---------------------------------------------------------- 
    #FUNCTION CopyFiles ---- 
    Function CopyFiles { #Unzip the files and copy in temp folders $shell_app= New-Object -com shell.application $files = Get-ChildItem -Path $srcZipFilepath foreach($file in $files) { $zip_files = $shell_app.namespace($file.FullName) if (!(Test-Path -path $tempPath)) { New-Item $tempPath -type directory } $copyHere = $shell_app.namespace($tempPath) $copyHere.Copyhere($zip_files.items()) } CopyFilesToFolders $tempPath } CopyFiles Write-Host "SCRIPT FINISHED" 
    Tuesday, December 30, 2014 10:37 AM