2

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?

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
  • 1
    Haha. Just found the problem. It's a typo "BackupDirectoy" - forgot an 'r'. Damn. I've spent 2 hours on this problem – Victor Hurdugaci Nov 12 '09 at 20:33
  • 6
    You can avoid these kinds of problems by using Set-StrictMode cmdlet at the start of your script. It will not let you use any variables you have not declared (it would have caught this error). – x0n Nov 12 '09 at 23:16

1 Answers1

5
$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
  • 194,368
  • 42
  • 353
  • 369