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?

2
  • 1
    Haha. Just found the problem. It's a typo "BackupDirectoy" - forgot an 'r'. Damn. I've spent 2 hours on this problem Commented Nov 12, 2009 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). Commented Nov 12, 2009 at 23:16

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Actually I want the paths to be relative to script location. The current dir in PowerShell is set to x\Script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.