3

I saw this already Fast and simple binary concatenate files in Powershell

I'm not interested by the answer above I'm interested about what's wrong with syntax below :

when I call a cmd.exe command like copy /b:

function join-file {
   copy /b $($args[0])+$($args[1]) $($args[2])
}

I get an error Copy-Item : A positional parameter cannot be found

mklement0
  • 382,024
  • 64
  • 607
  • 775
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

4

As the error alludes to, copy is actually just an alias for Copy-Item and it does not have a /b parameter. You can call cmd to use its copy command.

function join-file {
   cmd /c copy /b $($args[0])+$($args[1]) $($args[2])
}
mklement0
  • 382,024
  • 64
  • 607
  • 775
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
2

Note: This answer complements Doug Maurer's helpful answer, which provides an effective solution (for file names without spaces).

There's a subtlety in how PowerShell parses unquoted compound tokens such as $($args[0])+$($args[1]) (by compound token I mean directly concatenated distinct syntax constructs):

$($args[0])+$($args[1]) results in two arguments[1] - although with the specific command at hand (cmd.exe's internal copy command) that happens not to be a problem:

  • Argument 1: The value of $($args[0])

  • Argument 2: A verbatim + directly followed by the value of $($args[1])

To avoid this problem, enclose the whole compound token in "...", so as to predictably treat it as an expandable string.


The upshot:

  • To be safe, use double-quoting ("...") explicitly to enclose compound tokens that involve variable references or subexpressions.

  • By contrast, to reference a variable or even method call in isolation, neither quoting nor enclosing in $(...), the subexpression operator, are needed.

Applied naively to your command (see the better solution below):

# Note: See better solution below.
function join-file {
   # Note the "..." around the first argument, and the absence of quoting
   # and $(...) around the second.
   cmd /c copy /b "$($args[0])+$($args[1])" $args[2]
}

However, if $args[0] or $($args[1]) contained spaces, the copy command would malfunction; it is therefore more robust to pass the file names and the + as separate arguments, which copy also supports:

function join-file {
   # Pass the arguments individually, which obviates the need for quoting
   # and $(...) altogether:
   cmd /c copy /b $args[0] + $args[1] $args[2]
}

[1] You can verify this as follows: $arr='foo', 'bar'; cmd /c echo $($arr[0])+$($arr[1]), which yields: foo +bar (note the space).

mklement0
  • 382,024
  • 64
  • 607
  • 775