简体   繁体   中英

How to copy zip folder in 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!

  1. 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
}
  1. 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
}
  1. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM