4

I have created a powershell script that will copy selected files to predetermined location.
I have created a batch file to call the Powershell script from Windows Explorer "Send to" menu.
The batch file sends the selected file name as a parameter to the script. The file names can contain spaces. The batch file code is

powershell "C:\copycommand.ps1 '%1'"

When multiple files are selected I want to send all the file names as an array to the powershell script.
I have written the powershell script to accept arrays but I am not able to send the file names as an array from the batch file.

The below batch script sends all the filenames to the batch file but fails when the filename has spaces.

Powershell "C:\copycommand.ps1" %*

Is there a way in which I can send all the filenames to the powershell script.

ManojRK
  • 952
  • 9
  • 22
  • Can you show an example of `write-host $args`, and maybe `write-host $argument1` and `write-host $argument2`, to show how the powershell script receives `%*`? What I'm interested to see is whether each element is enclosed in quotation marks. – rojo Apr 10 '13 at 18:30
  • `write-host $args` returns the filenames in an array. However if the file name contains spaces the filename is split into two different array items. E.g. `C:\Program` and `Files\Sample.txt`. – ManojRK Apr 11 '13 at 08:57
  • Does this apply? http://stackoverflow.com/questions/5185030/drag-and-drop-batch-file-for-multiple-files ... getting a number of files into a batch file in one hit – AjV Jsy Apr 11 '13 at 09:36
  • @MrFuzzyButton - If I use the answer in the above link, it would still call the powershell script once for each file. I want to send all the filenames in one go. – ManojRK Apr 11 '13 at 10:01
  • There was something in that link, in one of the answers, about using the `!cmdcmdline!` variable to process the entire commandline string in one go, including all the individual file names. See also the answer at http://stackoverflow.com/questions/8547676/droplet-batch-script-filenames-containing-ampersands - looks like a useful thing, that `!cmdcmdline!` (although I haven't played with it myself). Just offered as something to experiment with. All the best. – AjV Jsy Apr 11 '13 at 10:17
  • This can be done without batch file, by directly invoking `PowerShell.exe` through the shortcut in the "SendTo" folder, see https://stackoverflow.com/a/72648180/7571258 – zett42 Jun 16 '22 at 16:05

1 Answers1

6

My bat file looks like (the last line is only for debugging, you can remove it):

@echo off
for %%x in (%*) do ( 
    powershell.exe  -Command "C:\copycommand.ps1 '%%x'"
)

powershell.exe  -noexit

My powershell script is:

$args | %{ Write-host "do copy $_"}

Example output is:

do copy C:\Users\Public\Desktop\SRWare Iron.lnk
do copy C:\Users\Public\Desktop\Mozilla Firefox.lnk
do copy C:\Users\Piotrek\Desktop\Windows 7 USB DVD Download Tool.lnk

Updated version

Change bat file to:

@echo off
set myvar=
for %%x in (%*) do call :concat %%x
echo %myvar%
powershell.exe  -Command C:\copycommand.ps1 %myvar%

powershell.exe  -noexit
goto :eof

:concat
set myvar=%myvar% '%1'
goto :eof
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
  • Thank you, but is there a way to send all the file names in one call? I want to collect all the errors and display them at the end. – ManojRK Apr 11 '13 at 08:38
  • Is it possible to get this to support file names with single quotes as well? – Jim Nov 20 '21 at 13:33