xil3
xil3

Reputation: 16449

Why mklink /j works when manually entering but not when run using exec.Command?

If I manually run the following command in Windows, it runs fine and creates the Junction Directory.

mklink /j "c:\Users\userid\OneDrive - Enterprise 365\backup\C\Users\userid\test" c:\Users\userid\test

But, if I do it programmatically with Go, using the following code, it comes back with Local NTFS volumes are required to complete the operation.

mklink := "mklink /j \"c:\\Users\\userid\\OneDrive - Enterprise 365\\backup\\C\\Users\\userid\\test\\\" c:\\Users\\userid\\test"

cmd := exec.Command("cmd", "/c", mklink)
out, err = cmd.CombinedOutput()

I've tried it a few different ways, but always getting the same result.

Any ideas?

Upvotes: 0

Views: 2729

Answers (2)

BitDreamer
BitDreamer

Reputation: 11

I noticed the version for Go, you have a trailing backslash after 'test' in the OneDrive path. This could be a subtle Windows behavior.

Another possibility - "mklink /j" requires an elevated command prompt. If your Go script is not running in such an Admin-mode window, it cannot create a Junction Point (or a hard link to a file).

Upvotes: -1

xil3
xil3

Reputation: 16449

I was able to get it working by using PowerShell instead.

mklink := `New-Item -ItemType Junction -Path "c:\\Users\userid\OneDrive - 
Enterprise 365\backup\C\Users\userid\test" -Target c:\\Users\userid\test`

cmd := exec.Command("PowerShell", "-Command", mklink)
out, err = cmd.CombinedOutput()

I'm assuming the issue was the way that GoLang was sending the embedded quotes - cmd was interpreting them incorrectly. PowerShell seems to be a lot more 'forgiving'.

Upvotes: 0

Related Questions