-1

I am trying to perform a Robocopy of a file. The command I'm using (below) works when the "filename" variable does not contain spaces. How can I write this command to ignore spaces in this variable?

System.Diagnostics.Process.Start("robocopy.exe", 
                   $@"X: ""C:\users\username\desktop\test"" {filename}").WaitForExit();

Please See my comments below (i.e.; dguth8)

dguth8
  • 29
  • 1
  • 5
  • 1
    Possible duplicate of [How do you handle spaces in variables when using C# interpolation?](https://stackoverflow.com/questions/50009781/how-do-you-handle-spaces-in-variables-when-using-c-sharp-interpolation) – maccettura Apr 24 '18 at 21:28
  • If the [question you asked an hour ago](https://stackoverflow.com/questions/50009781/how-do-you-handle-spaces-in-variables-when-using-c-sharp-interpolation) didn't give you a complete answer, dont mark it as the and dont ask another question. Edit your existing question – maccettura Apr 24 '18 at 21:34
  • 1
    @maccettura: No, that one was (wrongly) asking about spaces, now OP has realized his problem is adding quotes. It's a bit of a peculiar case, combining C# string interpolation with verbatim literals. – Ben Voigt Apr 24 '18 at 21:35
  • 2
    @maccettura: Clarifying an older question is good, but moving the goalposts is not. It's alright to ask a followup question (would have been good to link the earlier one though for background) – Ben Voigt Apr 24 '18 at 21:36
  • @BenVoigt One of the answers and most of the comments address the quotes on the previous question. These two questions are almost verbatim the same. OP even asks this: `"Normally, I would apply quotes around a path containing spaces, to counter this (in other languages like PowerShell). How do you do something similar with C# interpolation."` in the last question – maccettura Apr 24 '18 at 21:37
  • @maccettura: Not a single answer on the earlier question can work with verbatim literals. Adding quotes in a verbatim literal works different from adding one in an ordinary string (with backslash escapes). – Ben Voigt Apr 24 '18 at 21:40
  • @BenVoigt Fair enough, but if OP just applied the same logic they are literally already doing (doubling the double quotes): `""{filename}""` [they would get the result they want](https://dotnetfiddle.net/MFpPcs) right? Maybe this should be closed for being a "typo" – maccettura Apr 24 '18 at 21:42
  • @maccettura: I would expect that to work fine, yes. – Ben Voigt Apr 24 '18 at 23:16
  • Apologies, I'm new to using this forum, and wasn't sure whether to use a Comment to clarify, or open a new question. Technically there was a variation from the previous command (i.e.; also using @), so I wasn't sure whether it was appropriate to add to an existing answered question (and I just wasn't sure whether anyone would be able to see my comment as an actual question). – dguth8 Apr 25 '18 at 14:41
  • For future reference, is there any documentation on how to link to a previous ticket, or proper forum etiquette perhaps? Anyway, I've tried a variety of different combinations, and the only way I seem to be able to get the Robocopy to work is in the format I listed at the top of this question (i.e.; using both $ and @ prepended to the string). And that works, as long as there are no spaces in the filename. However, I run into a similar issue with spaces not being seen correctly when using the Robocopy syntax. – dguth8 Apr 25 '18 at 14:42
  • Wrapping the text in additional double quotes throws a flag in Visual Studio and prevents build and run. One more note, filename is a variable that is tied to a DataGridView row/cell. So I don't have the actual path typed out as a string. Ex: string fileName = selectedRow.Cells[1].Value.ToSTring(); – dguth8 Apr 25 '18 at 14:42

1 Answers1

0

I found the solution for this myself. Just in case anyone else is trying something similar, here's the command I had to use:

Note: Essentially, I had to combine the 3 arguments for Robocopy into a single string, then use the approach addressed in the other question "How do you handle spaces in variables when using C# interpolation?", meaning escaping quotes around the variable.

System.Diagnostics.Process.Start("robocopy.exe", $"X:\ C:\\users\\usersname\\Desktop\\FolderName \"{filename}\"").WaitForExit();

dguth8
  • 29
  • 1
  • 5