How to copy zip folder in powershell - powershell

How to copy the zip folder using user input values instead of hardcoded and also after placing the zip in visual studio distination folder need to clean and build the .csproj
$source = "C:\Users\name\Downloads"
$archive = "C:\Users\name\Downloads"
$Name = "xyz.zip"
$destination = "D:\repo"
$ArchiveFile = Join-Path -Path $archive -ChildPath $Name
Copy-Item -Path $ArchiveFile -Destination $destination -Force

Parameterize your variables!
Enclose you code in a function definition,
function Copy-BuildArchive
{
param()
$archive = "C:\Users\name\Downloads"
$Name = "xyz.zip"
$destination = "D:\repo"
$ArchiveFile = Join-Path -Path $archive -ChildPath $Name
Copy-Item -Path $ArchiveFile -Destination $destination -Force
}
Take the variables that you wish to substitute with user input, and move them into the param block:
function Copy-BuildArchive
{
param(
[string]$Name,
[string]$Archive,
[string]$Destination
)
$ArchiveFile = Join-Path -Path $Archive -ChildPath $Name
Copy-Item -Path $ArchiveFile -Destination $Destination -Force
}
Add some rudimentary input validation, and perhaps some default values:
function Copy-BuildArchive
{
param(
[Parameter(Mandatory = $true)]
[string]$Name,
[string]$Archive = $(Join-Path $HOME "Downloads"),
[string]$Destination = $(Join-Path $HOME "Documents")
)
$ArchiveFile = Join-Path -Path $Archive -ChildPath $Name
Copy-Item -Path $ArchiveFile -Destination $Destination -Force
}
Congratulations - you just turned your script into a parameterized function!
Now, after executing the function definition, you can do:
Copy-BuildArchive -Name project.zip -Archive C:\build\path -Destination C:\output\path
or
Copy-BuildArchive -Name somethingElse.zip # using default values for Archive and Destination

Related

Powershell 2.0 extract certain files from zip (include subdirectories)

Apologies, this question is scattered on the internet but I have yet to find a satisfactory answer that uses only Powershell 2.0 (with .NET v3.5) - no external libraries or programs
I'm using the following code to extract log.txt from ZipFile.zip (no matter log.txt's location)
$Destination = (new-object -com shell.application).NameSpace('C:\ZipExtractDir')
$ZipFile = (new-object -com shell.application).NameSpace('C:\ZipFile.zip')
$Destination.CopyHere(($Zipfile.Items() | where-object {$_.Name -like '*log.txt'}), 1044)
Works if log.txt is in directory root \log.txt
Fails if log.txt is in a subdirectory \Subfolder\log.txt
Fails if referencing the literal (.zip) path
{$_.Name -Like '*Subfolder\log.txt'} (both double & single quotes fail)
Have tried using -eq -like -contains '' "" $_.FullName
I'm quite certain that I'm filtering incorrectly - can anyone help with this code so that it will parse subdirectories as well?
Similar to what you have already done, you can set up the Shell.Application namespaces like this. Then you can copy the extracted directory to the destination path.
$zipFilePath = "Zipfile.zip"
$destinationPath = "C:\Users\Public\Downloads"
$zipfile = (New-Object -Com Shell.Application).NameSpace($zipFilePath)
$destination = (New-Object -Com Shell.Application).NameSpace($destinationPath)
$destination.CopyHere($zipfile.Items())
Then to list the log.txt files, we can contruct the full extracted path with Join-Path. This basically just appends the zip file name from System.IO.Path.GetFileNameWithoutExtension() to the destination path. Then just use Get-ChildItem to list the files recursively with the -Recurse and -Filter switches.
$extractedPath = Join-Path -Path $destinationPath -ChildPath ([System.IO.Path]::GetFileNameWithoutExtension($zipFilePath))
Get-ChildItem -Path $extractedPath -Filter log.txt -Recurse
And to test this for PowerShell 2.0 we can use -version 2 with powershell.exe:
powershell.exe -version 2 .\test.ps1
UPDATE
If you want to inspect files before extracting, you'll need to recurse the directories yourself. Below is a demo of how this can be done.
function New-ZipChildRootFolder
{
param
(
[string]$DestinationPath,
[string]$ZipFileName
)
$folderPath = Split-Path -Path $ZipFileName -Leaf
$destination = (New-Object -ComObject Shell.Application).NameSpace($DestinationPath)
$destination.NewFolder($folderPath)
}
function Get-ZipChildItems
{
param
(
[string]$ZipFilePath,
[string]$DestinationPath
)
$zipfile = (New-Object -ComObject Shell.Application).NameSpace($ZipFilePath)
$zipFileName = [System.IO.Path]::GetFileNameWithoutExtension($ZipFilePath)
Write-Output "Create root zip folder : $zipFileName"
New-ZipChildRootFolder -DestinationPath $DestinationPath -ZipFileName $zipFileName
foreach ($item in $zipFile.items())
{
Get-ZipChildItemsRecurse -Items $item -DestinationPath $DestinationPath -ZipFileName $zipFileName
}
}
function Get-ZipChildItemsRecurse
{
param
(
[object]$Items,
[string]$DestinationPath,
[string]$ZipFileName
)
foreach ($file in $Items.getFolder.Items())
{
if ($file.IsFolder -eq $true)
{
Write-Output "Creating folder : $($file.Path)"
New-ZipChildFolder -Folder $file -DestinationPath $DestinationPath -ZipFileName $ZipFileName
Get-ZipChildItemsRecurse -Items $file -DestinationPath $DestinationPath -ZipFileName $ZipFileName
}
else
{
$filename = Split-Path -Path $file.Path -Leaf
if ($filename -eq "log.txt")
{
Write-Output "Copying file : $($file.Path)"
New-ZipChildFile -File $file -DestinationPath $DestinationPath -ZipFileName $ZipFileName
}
}
}
}
function New-ZipChildFile
{
param
(
[object]$File,
[string]$DestinationPath,
[string]$ZipFileName
)
$destination = New-Object -ComObject Shell.Application
$items = $File.Path.Split("\")
$zipRootIndex = [array]::IndexOf($items, $ZipFileName)
$path = $items[$zipRootIndex..($items.Length - 2)] -join "\"
$fullPath = Join-path -Path $DestinationPath -ChildPath $path
$destination.NameSpace($fullPath).CopyHere($File)
}
function New-ZipChildFolder
{
param
(
[object]$Folder,
[string]$DestinationPath,
[string]$ZipFileName
)
$destination = New-Object -ComObject Shell.Application
$items = $Folder.Path.Split("\")
$zipRootIndex = [array]::IndexOf($items, $ZipFileName)
$folders = $items[$zipRootIndex..($items.Length - 1)]
$currentFolder = $DestinationPath
foreach ($folder in $folders)
{
$destination.NameSpace($currentFolder).NewFolder($folder)
$currentFolder = Join-Path -Path $currentFolder -ChildPath $folder
}
}
Usage:
$zipFilePath = "C:\Zipfile.zip"
$destinationPath = "C:\Users\Public\Downloads"
Get-ZipChildItems -ZipFile $zipFilePath -DestinationPath $destinationPath

Powershell move file with backup (like mv --backup=numbered)

I'm looking if there's a PS command that'd be equal to mv --backup=numbered, and can't find anything.
In essence, move 'file' to 'file.old', but if 'file.old' exists, 'file' should be moved to 'file.old.2'.
For now the closest I found is from this link: https://www.pdq.com/blog/copy-individual-files-and-rename-duplicates/:
$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"
If (Test-Path $DestinationFile) {
    $i = 0
    While (Test-Path $DestinationFile) {
        $i += 1
        $DestinationFile = "C:\Temp\NonexistentDirectory\File$i.txt"
    }
} Else {
    New-Item -ItemType File -Path $DestinationFile -Force
}
Copy-Item -Path $SourceFile -Destination $DestinationFile -Force
It seems quite awful to have this amount of code. Is there anything simpler available?
Indeed there is no built-in function to do that. However, it should not be a problem to use a function of your own for that purpose.
How about this:
function Copy-FileNumbered {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
[string]$SourceFile,
[Parameter(Mandatory = $true, Position = 1)]
[string]$DestinationFile
)
# get the directory of the destination file and create if it does not exist
$directory = Split-Path -Path $DestinationFile -Parent
if (!(Test-Path -Path $directory -PathType Container)) {
New-Item -Path $directory -ItemType 'Directory' -Force
}
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($DestinationFile)
$extension = [System.IO.Path]::GetExtension($DestinationFile) # this includes the dot
$allFiles = Get-ChildItem $directory | Where-Object {$_.PSIsContainer -eq $false} | Foreach-Object {$_.Name}
$newFile = $baseName + $extension
$count = 1
while ($allFiles -contains $newFile) {
$newFile = "{0}({1}){2}" -f $baseName, $count, $extension
$count++
}
Copy-Item -Path $SourceFile -Destination (Join-Path $directory $newFile) -Force
}
This will create a new file in the destination like File(1).txt
Of course, if you rather have names like File.2.txt, just change the format template "{0}({1}){2}" to "{0}.{1}{2}"
Use the function like
$SourceFile = "C:\Temp\File.txt"
$DestinationFile = "C:\Temp\NonexistentDirectory\File.txt"
Copy-FileNumbered -SourceFile $SourceFile -DestinationFile $DestinationFile

Add multiple folders in one zip file in Powershell

Perhaps my question can be a duplicate, but I'm new in powershell, and cannot figure out, what is wrong with my script, that zipping particular directories:
$path = "C:\backup\DEV82"
if(!(Test-Path -Path $path )){
New-Item -ItemType directory -Path $path
}
cd C:\inetpub\wwwroot\dev82\
$SOURCE = Get-ChildItem * -Directory|Where-Object {$_.FullName -match "App_Config|Resources|bin"}
$dtstamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $SOURCE)
{
$DESTINATION = Join-path -path $path -ChildPath "$dtstamp.zip"
If(Test-path $DESTINATION) {
Remove-item $DESTINATION
}
[io.compression.zipfile]::CreateFromDirectory($s.fullname, $DESTINATION)
}
If I execute command in $SOURCE variable, it gathers all required directories, which I want zip http://prntscr.com/j0sqri
$DESTINATION also returns valid value
PS C:\> $DESTINATION
C:\backup\DEV82\20180404_223153.zip
but right now only last folder (Resources) exists in zip file.
Ok, I rewrite my script, using, instead of Zipfile class, Compress-Archive with -Update ( -Update allows to add files\folders into existing archive )
$path = "C:\backup\DEV82"
if(!(Test-Path -Path $path )){
New-Item -ItemType directory -Path $path
}
cd C:\inetpub\wwwroot\dev82\
$SOURCE = Get-ChildItem * -Directory|Where-Object {$_.FullName -match "App_Config|Resources|bin"}
$dtstamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
$DESTINATION = Join-path -path $path -ChildPath "$dtstamp.zip"
Add-Type -assembly "system.io.compression.filesystem"
If(Test-path $DESTINATION) {
Remove-item $DESTINATION
}
Foreach ($s in $SOURCE)
{
Compress-Archive -Path $s.fullname -DestinationPath $DESTINATION -Update
}
$SOURCE is already just a list of folder names, so you don't need the FullName property here:
[io.compression.zipfile]::CreateFromDirectory($s.fullname, $DESTINATION)
Either remove it, or remove the Select-Object from the pipeline here:
$SOURCE = Get-ChildItem * -Directory |
Where-Object {$_.FullName -match "App_Config|Resources|bin"} |
Select-Object -ExpandProperty FullName

How do I move multiple files with the same name and extension that are in subfolders into one main folder [duplicate]

I have a powershell script that takes all of the files in one directory, renames the first one and moves it, and then goes to the next file. Sometimes there will be multiple files that get renamed to the same name (because of the system that it's going to, not ideal and will have to change) and was overwriting files that shouldn't have been getting overwritten with -force. I need all of the files to move but also have unique names so we have them available in the destination location. Is there an easy way to have it automatically rename so it would look like:
123.txt
123(1).txt
123(2).txt
or
123.txt
123_1.txt
123_2.txt
There's no built-in way to do that. Give this a try:
$src = "d:\temp"
$dest = "d:\temp1"
$num=1
Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName
}
#Fixed solution basing on previous answer (variable $num moved into for each loop):
$src = "d:\temp"
$dest = "d:\temp1"
Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$num=1
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName
}
So I'm way late to the party here but... I kinda liked the responses just didn't like the lack of it being in a function so... I modified it for re-usability.
I added the Name field because I'm using this as part of another process where I am using a regex substring process to take a file name which contains <owner>-<date_string>-<hash>.<extension> and moving them into a <file_path>/<owner>/<date_string>.<extension> format.
function Move-Item-AutoRename {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[String]$Source,
[Parameter(Mandatory = $true)]
[String]$Destination,
[Parameter(Mandatory = $true)]
[String]$Name
)
PROCESS {
$count = 1
[System.IO.FileInfo]$nextName = Join-Path -Path $Destination -ChildPath $Name
while (Test-Path -Path $nextName) {
$nextName = Join-Path -Path $Destination ($Name.Split(".")[0] + "_$($count)" + $nextName.Extension)
$count += 1
}
Move-Item -Path $Source -Destination $nextName
}
}
Since I am sure others may be interested in the other part of this solution I will include it just for sharing sake.
function Import-UnsortedFiles {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[String]$Source,
[Parameter(Mandatory = $true)]
[String]$Destination
)
PROCESS {
Get-ChildItem -Path $Source -Include *.xml, *.json | ForEach-Object {
$results = $_.Name | Select-String -Pattern '^(.*)-([0-9]*_[0-9]*)-.*(\..*)$'
[System.IO.FileInfo] $dest = (Join-Path -Path $Target -ChildPath "$($results.Matches.Groups[1])/$($results.Matches.Groups[2])$($results.Matches.Groups[3])");
$test = (Test-Path -Path $dest.Directory.FullName -PathType Container)
if ($test -eq $false) {
New-Item -Path $dest.Directory.FullName -ItemType Directory;
}
Move-Item-AutoRename -Source $_.FullName -Destination $dest.Directory.FullName -Name $dest.Name
}
}
}
Call this by invoking Import-UnsortedFiles -Source $Source -Destination $Target

Powershell Move-Item Rename If File Exists

I have a powershell script that takes all of the files in one directory, renames the first one and moves it, and then goes to the next file. Sometimes there will be multiple files that get renamed to the same name (because of the system that it's going to, not ideal and will have to change) and was overwriting files that shouldn't have been getting overwritten with -force. I need all of the files to move but also have unique names so we have them available in the destination location. Is there an easy way to have it automatically rename so it would look like:
123.txt
123(1).txt
123(2).txt
or
123.txt
123_1.txt
123_2.txt
There's no built-in way to do that. Give this a try:
$src = "d:\temp"
$dest = "d:\temp1"
$num=1
Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName
}
#Fixed solution basing on previous answer (variable $num moved into for each loop):
$src = "d:\temp"
$dest = "d:\temp1"
Get-ChildItem -Path $src -Filter *.txt -Recurse | ForEach-Object {
$num=1
$nextName = Join-Path -Path $dest -ChildPath $_.name
while(Test-Path -Path $nextName)
{
$nextName = Join-Path $dest ($_.BaseName + "_$num" + $_.Extension)
$num+=1
}
$_ | Move-Item -Destination $nextName
}
So I'm way late to the party here but... I kinda liked the responses just didn't like the lack of it being in a function so... I modified it for re-usability.
I added the Name field because I'm using this as part of another process where I am using a regex substring process to take a file name which contains <owner>-<date_string>-<hash>.<extension> and moving them into a <file_path>/<owner>/<date_string>.<extension> format.
function Move-Item-AutoRename {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[String]$Source,
[Parameter(Mandatory = $true)]
[String]$Destination,
[Parameter(Mandatory = $true)]
[String]$Name
)
PROCESS {
$count = 1
[System.IO.FileInfo]$nextName = Join-Path -Path $Destination -ChildPath $Name
while (Test-Path -Path $nextName) {
$nextName = Join-Path -Path $Destination ($Name.Split(".")[0] + "_$($count)" + $nextName.Extension)
$count += 1
}
Move-Item -Path $Source -Destination $nextName
}
}
Since I am sure others may be interested in the other part of this solution I will include it just for sharing sake.
function Import-UnsortedFiles {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, ValueFromPipeline)]
[String]$Source,
[Parameter(Mandatory = $true)]
[String]$Destination
)
PROCESS {
Get-ChildItem -Path $Source -Include *.xml, *.json | ForEach-Object {
$results = $_.Name | Select-String -Pattern '^(.*)-([0-9]*_[0-9]*)-.*(\..*)$'
[System.IO.FileInfo] $dest = (Join-Path -Path $Target -ChildPath "$($results.Matches.Groups[1])/$($results.Matches.Groups[2])$($results.Matches.Groups[3])");
$test = (Test-Path -Path $dest.Directory.FullName -PathType Container)
if ($test -eq $false) {
New-Item -Path $dest.Directory.FullName -ItemType Directory;
}
Move-Item-AutoRename -Source $_.FullName -Destination $dest.Directory.FullName -Name $dest.Name
}
}
}
Call this by invoking Import-UnsortedFiles -Source $Source -Destination $Target