返回列表 发帖

[问题求助] Powershell能否将文件列表写入剪切板

本帖最后由 523066680 于 2014-7-5 20:43 编辑

想实现一个功能:在某个文件夹鼠标右键粘贴来自于不同目录的Files
这个操作在Explorer中操作不成功(或者说是不同的目录复制粘贴要分批操作),考虑用脚本将文件列表一次写入剪切板,
所以咨询下,Powershell可以向剪切板写入文件列表吗?

用伪代码表达我的问题就是,能不能这样:
clipboard.setAs(
       ("C:\movie.mkv",
        "E:\multi.avi",
        "D:\demo.flac"),  "FILE"
);
[url=][/url]

dir | clip.exe
PowerShell 群:271143343

TOP

回复 2# victorwoo


    dir不能满足“自于不同目录的Files”这个要求吧

TOP

本帖最后由 CrLf 于 2014-7-6 04:14 编辑

如果只是文件名那好办,不用 clip.exe 的办法是:
(以 powershell -sta 启动后)
Add-Type -Assembly PresentationCore
$a='a.txt','b.txt','c.txt'
[Windows.Clipboard]::SetText($a -join "`n")COPY
复制文件可以这样:
$filelist = 'c:\a.txt','d:\b.txt'
$col = New-Object Collections.Specialized.StringCollection
foreach($file in $filelist){$col.add($file)}
[Windows.Clipboard]::setfiledroplist($col)COPY
----------------------------------------------------------------
不过这种事其实用 ahk 更合适,复制文件名:
Clipboard:= "c:\a.txt`r`nd:\b.txt"COPY
复制文件:
FileAppend, %ClipboardAll%, c:\a.txt
FileAppend, %ClipboardAll%, d:\b.txtCOPY

TOP

回复 4# CrLf


    ahk这么方便论坛不设一个AHK的版块?
[url=][/url]

TOP

本帖最后由 523066680 于 2014-7-11 14:29 编辑

回复 4# CrLf


    由衷感谢CRLF童鞋,
FileAppend, %ClipboardAll%, C:\Find.Me.Guilty.2006.720p.BluRay.x264-WiKi.mkv
FileAppend, %ClipboardAll%, C:\Survival.Code.aka.Borealis.2013.SUB.720p.WEB-DL.x264.AC3-SmY.mkv

怪我没去看帮助文件,执行这两句话后我的电影变成1KB了…… T_T

这东西的作用
Writes text to the end of a file (first creating the file, if necessary).


ahk的那些功能好像也就和nircmd差不多?
[url=][/url]

TOP

回复 6# 523066680


    噗...我看手册复制偏了...
忽然发现 ahk 好像没法直接复制文件,请教了一下 tmplinshi 大师,得到了一大坨代码:
#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%
SetBatchLines -1
fileList := "
(LTrim
js.txt
e:\我的文档\桌面\vcode\18.jpg
)"
FileToClipboard(fileList)
FileToClipboard(PathToCopy, Method="copy")
{
; 展开为完整路径
Loop, Parse, PathToCopy, `n, `r
Loop, %A_LoopField%
PathToCopy_Full .= "`n" A_LoopFileLongPath
PathToCopy := Trim(PathToCopy_Full, "`n")
FileCount:=0
PathLength:=0
; Count files and total string length
Loop,Parse,PathToCopy,`n,`r
{
FileCount++
PathLength += StrLen(A_LoopField)
}
pid:=DllCall("GetCurrentProcessId","uint")
hwnd:=WinExist("ahk_pid " . pid)
; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
pPath := DllCall("GlobalLock","UPtr",hPath)
NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
Offset:=0
Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
DllCall("GlobalUnlock","UPtr",hPath)
DllCall("OpenClipboard","UPtr",hwnd)
DllCall("EmptyClipboard")
DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP
; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
str := DllCall("GlobalLock","UPtr",mem)
if (Method="copy")
DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
else if (Method="cut")
DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
else
{
DllCall("CloseClipboard")
return
}
DllCall("GlobalUnlock","UPtr",mem)
cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
DllCall("CloseClipboard")
return
}COPY

TOP

回复 6# 523066680


    ahk 比 nircmd 强大太多了好吧

TOP

回复 7# CrLf


    屌炸天
[url=][/url]

TOP

ahk确实不错的。
呵呵,能够得到 tmplinshi大神的指点,不错啊。

TOP

返回列表