When you right-click a file within Windows Explorer and choose Copy, you aren't copying that file's path as text data. Try it. Right-click-copy a file on your Desktop and try to paste in Notepad or some other text editor. Nothing, right?
No, you need to use some .NET methods to copy the file pointers in a way that Windows will let you Paste to perform a file copy action. This is done by invoking the Clipboard.SetFileDropList()
method.
Here's a batch + PowerShell script solution that exposes this method. Save it with a .bat extension.
<# : batch portion
@echo off & setlocal
set "filemask=*.out"
powershell -STA -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin PowerShell hybrid code #>
$collection = new-object Collections.Specialized.StringCollection
gci $env:filemask | %{ $collection[$collection.Add($_.FullName)] }
Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Clipboard]::SetFileDropList($collection)
Following Paul's request, a couple of minor tweaks will allow you to select files via regexp, so you can match multiple extensions.
<# : batch portion
@echo off & setlocal
set "rxp=\.(out|dat|log)$"
powershell -STA -noprofile "iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin PowerShell hybrid code #>
$collection = new-object Collections.Specialized.StringCollection
gci | ?{ $_.Name -match $env:rxp } | %{ $collection[$collection.Add($_.FullName)] }
Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Clipboard]::SetFileDropList($collection)
And just because I felt like accepting LinuxDisciple's challenge, here's a solution that accepts file masks from the command line.
<# : batch portion
@echo off & setlocal
if "%~1"=="" (
echo Usage: %~nx0 filemask [filemask [filemask [...]]]
echo example: %~nx0 *.jpg *.gif *.bmp
goto :EOF
)
(for %%I in ("%~f0";%*) do @for %%# in ("%%~I") do @echo(%%~f#) | ^
powershell -STA -noprofile "$argv = $input | ?{$_}; iex (${%~f0} | out-string)"
goto :EOF
: end batch / begin powershell #>
$col = new-object Collections.Specialized.StringCollection
$argv[1..($argv.length-1)] | ?{$_.length -gt 3 -and (test-path $_)} | %{$col[$col.Add($_)]}
if ($col.Count) {
Add-Type -AssemblyName System.Windows.Forms
[Windows.Forms.Clipboard]::SetFileDropList($col)
}