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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With