2

I need help ending a debate with a colleague. I have the following script:

$source = '\\serverA\folder'
$destination = '\\serverB\folder'
Copy-Item -Path "$source\myFile.txt" -Destination $destination

My coworker claims that I need to have a backslash added to the end of my destination parameter so that it reads $destination = '\\serverB\folder\', otherwise I may have unforeseen complications.

So, is the backslash providing any vital importance in the precise syntax I have above? From what I see online, it seems to be arbitrary depending on the coder.

Mr. Mike
  • 229
  • 4
  • 13

2 Answers2

3

Your coworker might be confusing PowerShell with VBScript. VBScript's CopyFile and CopyFolder methods require a trailing backslash if the destination is a folder. PowerShell's Copy-Item cmdlet doesn't care whether the destination does or doesn't have a trailing backslash as long as the folder exists.

He's not entirely wrong, though. If the destination folder doesn't exist a trailing backslash in the destination path does make a difference:

PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize

Mode  FullName
----  --------
d---- C:\Temp\a
-a--- C:\Temp\a\foo.txt

PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b'
PS C:\Temp> ls -r | select Mode, FullName | ft -AutoSize

Mode  FullName
----  --------
d---- C:\Temp\a
-a--- C:\Temp\b
-a--- C:\Temp\a\foo.txt

PS C:\Temp> Remove-Item 'C:\temp\b'
PS C:\Temp> Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\b\'
Copy-Item : The filename, directory name, or volume label syntax is incorrect.
...

A non-existing destination path with a trailing backslash throws an error, whereas without a trailing backslash the destination is created as a file.

However, IMO a better way to deal with missing destination folders is to actually check for their presence and create them if they don't exist:

if (-not (Test-Path -LiteralPath $destination -Type Container)) {
    New-Item -Type Directory -Path $destination | Out-Null
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Both your answer and the answer by @Zucchini proved helpful. I think your post would benefit others from a broader perspective. I believe that my new mode of thinking will be, "if it represents a folder path, end in a slash", since PowerShell seems to not care about doubled slashes, but DOES care if the slash is missing. – Mr. Mike May 16 '17 at 15:52
  • 1
    That was not what I meant to imply. In fact, I recommend *not* putting trailing backslashes in path strings. Do proper validation (as suggested in my answer) and construct paths via the [`Join-Path`](http://stackoverflow.com/a/17146630/1630171) cmdlet. – Ansgar Wiechers May 16 '17 at 15:59
  • Being thorough, why recommendation against trailing slashes? `Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\\b\'` performs favorably despite the typo. Also, thank you for the advice on `Join-Path`. Part of the reason for asking the original question is because I'm fighting against a code standardization, and they have at least loosened up about letting me use that cmdlet. I would normally perform path validation or error handling, but in my specific context the errors are handled by a parent environment that relies on catching and responding to them. I apologize if that sidetracks the Q/A – Mr. Mike May 18 '17 at 22:36
  • The reason should become obvious when you follow the link in my previous comment. – Ansgar Wiechers May 19 '17 at 08:32
  • I have followed your link. However, outside of revealing the code is receiving poor syntax, it does not express why `Copy-Item 'C:\Temp\a\foo.txt' 'C:\temp\\b\'` works or what the implications of that may be. I'm struggling with getting any answers out of my internet searches, unfortunately. – Mr. Mike May 19 '17 at 17:53
1

I do a lot of file handling in powershell and i always leave out the last \ in my paths, but i'm not sure if it actually makes a difference. What is your coworkers exact reasoning?

You could also go with the idea that paths ending in a slash are directories and paths not ending in one are files. I'm pretty sure it's a matter of opinion.

Edit, seems it has been discussed before. Should a directory path variable end with a trailing slash?

Community
  • 1
  • 1
Zucchini
  • 459
  • 6
  • 16
  • I read through that discussion, and a member wrote "At least on the OSes I commonly use, doubling the slash causes no problems." This seems to hold true in PowerShell as well – Mr. Mike May 16 '17 at 15:44