views:

229

answers:

1

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
}

BackupCurrentVersion

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

What is the problem?

+3  A: 
$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"
Keith Hill
Actually I want the paths to be relative to script location. The current dir in PowerShell is set to x\Script
Victor Hurdugaci