开发者

PowerShell Copy-Item what I'm doing wrong?

开发者 https://www.devze.com 2022-12-11 11:47 出处:网络
Given the dir structure: x\\Code x\\Script\\Backup.ps1 Backup.ps1 contains: $BackupDirectoy= \"..\\Backup\"

Given the dir structure:

x\Code
x\Script\Backup.ps1

Backup.ps1 contains:

$BackupDirectoy       = "..\Backup"
$CodeDirectory        = "..\Code"

function BackupCurrentVersion()
{
  New-Item $BackupDirectoy -type directory -force
  Copy-Item $CodeDirectory -destination $BackupDirectory -recurse
}

BackupCurre开发者_JAVA百科ntVersion

I'm doing something wrong because Code gets copied in x\Script\Code instead of x\Backup\Code

What is the problem?


$BackupDirectoy       = "..\Backup"
$CodeDirectory        = "..\Code"

These paths are going to be relative to the current dir your prompt is sitting in when you run the script. I suspect you don't want that but want to run relative to where the script is located. Try this if this is the case:

$ScriptDir      = Split-Path $MyInvocation.MyCommand.Path -Parent
$BackupDirectoy = "$ScriptDir\..\Backup"
$CodeDirectory  = "$ScriptDir\..\Code"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号