Not familiar with Windows, but my guess would be that forward slashes in the first argument might be parsed as being options to the xcopy
command. Any following arguments won't be handled as such, and will therefore work.
As for escaping, the issue isn't so much spawn
, but more the general way of escaping in literal Javascript strings, using \
. So if you use this:
spawn('xcopy', [ 'C:\my\path\myfile.test', ... ])
then this is what spawn
will receive (as arguments
):
{ '0': 'xcopy', '1': [ 'C:mypathmyfile.test' ] }
And similarly:
> console.log( 'C:\my\path\myfile.test' )
C:mypathmyfile.test
So if you want to add a backslash to a JS string, you need to escape it:
> console.log( 'C:\\my\\path\\myfile.test' )
C:\my\path\myfile.test
But this is only necessary for literal strings in your code (hardcoded in your JS). For strings that are either calculated or passed in some form (like from the command line in process.argv
), additional escaping isn't necessary.
Since spawn
is agnostic as to what you pass to it, it won't do any escaping itself.