使用Explorer上下文菜单将修改后的文件路径版本复制到剪贴板

时间:2012-02-07 19:03:33

标签: windows powershell explorer wsh windows-scripting

我得到的要求非常简单,但我正在努力筛选有关该主题的大量无关信息。

要求

我的PC和我的网络服务器之间有文件同步。基本上我需要能够右键单击本地文件并将等效的服务器路径(带有一些额外的字符串操作逻辑)复制到剪贴板。

方法

我想我需要做的是以下内容:

  1. 添加Windows资源管理器上下文菜单选项以执行WSH脚本,将完整文件路径和名称作为参数传递。
  2. 创建一个Windows Scripting Host脚本,该脚本将接受该参数,进行必要的字符串操作,并复制到剪贴板。
  3. 我可以处理的字符串操作很好(最好是在VBScript中)。这是将一个参数传递给WSH脚本,我无法找到任何关于它的信息。

    或者,我不介意是否使用PowerShell(如果适用)完成此操作,这样我就可以在学习它的同时学到更多知识。

    非常感谢提前。

4 个答案:

答案 0 :(得分:1)

您可以在VB脚本中访问参数:

WScript.Echo(WScript.Arguments(0))

注册shell上下文菜单命令时,可以通过注册来传递路径和文件名作为参数:

wscript.exe "C:\...full path...\myscript.vbs" "%1"

答案 1 :(得分:1)

好的,我已经在资源管理器上下文菜单中修改了一个VBS脚本,并允许您右键单击一个文件,将其相应的服务器URL复制到剪贴板。

'####################################################################
' If you sync files between your local PC and a web server you can use this
' script to right-click on one of those files to copy the corresponding server
' URL to your clipboard
'####################################################################
Option Explicit

'Local path to the directory that is being synchronised with the server
Const constRootWinPath = "C:\SyncedFiles"
'path to corresponding directory on the server
Const constRootServerPath = "/SyncedFiles/"
'Domain name of the server
Const constServerDomain = "http://mydomain.dom/"
'MAKE SURE TO INCLUDE LEADING AND TRAILING SLASHES ON ALL PATHS!!!!!

Dim objIE

' Parse the command line arguments
If WScript.Arguments.Count      <> 1 Then Syntax
If WScript.Arguments.Named.Count = 1 Then
    If WScript.Arguments.Named.Exists( "Register" ) Then
        Register
    ElseIf WScript.Arguments.Named.Exists( "Unregister" ) Then
        UnRegister
    Else
        Syntax
    End If
End If

' Check arguments. Text argument gets processed as a path. 
If WScript.Arguments.UnNamed.Count = 1 Then

    Dim strArgument
    strArgument = WScript.Arguments.Unnamed(0)

    'The file has to exist within a directory under constRootWinPath so that we know how to process the path
    If instr(trim(strArgument),trim(constRootWinPath)) > 0 Then
        'WScript.Echo """" & constRootWinPath & """ was found in """ & strArgument & """"
        SendToClipboard(ProcessLocalPathToServerPath(WScript.Arguments.Unnamed(0)))
    Else
        WScript.Echo """" & constRootWinPath & """ not found in """ & strArgument & """. Please make sure to edit the Const in the VBS file"
    End If
End If


Function ProcessLocalPathToServerPath(strLocalPath)
    Dim strProcessedPath, strFileName, strRelPathToRoot, strFileExtension

    'Get the filename
    strFileName = right(strLocalPath,len(strLocalPath)-InStrRev(strLocalPath,"\"))
    'WScript.Echo "strFileName: """ & strFileName & """"

    'Get the relative path to the root
    strRelPathToRoot = mid(strLocalPath,len(constRootWinPath),len(strLocalPath)-(len(constRootWinPath)+len(strFileName))+1) '+1 to get the trailing slash
    'Swap back slash for forward slash
    strRelPathToRoot = replace(strRelPathToRoot,"\","/")
    'WScript.Echo "strRelPathToRoot: """ & strRelPathToRoot & """"

    'Get the file extension
    strFileExtension = right(strFileName,len(strFileName)-InStrRev(strFileName,"."))
    'WScript.Echo "strFileExtension: """ & strFileExtension & """"

    'Process the paths depending on file type
    Select Case strFileExtension
        'send swf files to our wrapper viewer on the server
        Case "swf"
            strProcessedPath = constServerDomain & "flashviewer.asp?swf=" & constRootServerPath & strRelPathToRoot & strFileName
        'Use google viewer for supported file types
        Case "docx","doc","xls","xlsx","ppt","pptx","pdf","pages","ai","psd","tiff","dxf","svg","eps","ps","ttf","xps","zip","rar"
            strProcessedPath = "http://docs.google.com/viewer?url=" & constServerDomain & constRootServerPath & strRelPathToRoot & strFileName
        'direct file path
        Case else
            strProcessedPath = constServerDomain & constRootServerPath & strRelPathToRoot & strFileName
    End Select
    'WScript.Echo "strProcessedPath: """ & strProcessedPath & """"

    ProcessLocalPathToServerPath = strProcessedPath
End Function

' The Internet Explorer object is used, because WSH
' and VBScript don't support clipboard access by themselves.
Sub SendToClipboard(strToClipboard)
    Set objIE = CreateObject( "InternetExplorer.Application" )
    objIE.Navigate( "about:blank" )
    objIE.Document.ParentWindow.ClipboardData.SetData "text", strToClipboard
    objIE.Quit
    Set objIE = Nothing
End Sub 

Sub Register
    Dim wshShell

    Set wshShell = CreateObject( "WScript.Shell" )

    On Error Resume Next

    ' Add the required registry entries for files
    wshShell.RegWrite "HKEY_CLASSES_ROOT\*\shell\webists_serverpathtoclip\", "Copy Sever URL"
    wshShell.RegWrite "HKEY_CLASSES_ROOT\*\shell\webists_serverpathtoclip\command\", "wscript.exe """ & WScript.ScriptFullName & """ ""%L""", "REG_EXPAND_SZ"

    On Error Goto 0

    Set wshShell = Nothing
    WScript.Echo "Script successfully registered."
    WScript.Quit 0
End Sub


Sub UnRegister
    Dim wshShell

    Set wshShell = CreateObject( "WScript.Shell" )

    On Error Resume Next

    ' Remove the registry entries for the files menu
    wshShell.RegDelete "HKEY_CLASSES_ROOT\*\shell\webists_serverpathtoclip\command\"
    wshShell.RegDelete "HKEY_CLASSES_ROOT\*\shell\webists_serverpathtoclip\"

    ' Remove the registry entries for the folders menu
    ' wshShell.RegDelete "HKEY_CLASSES_ROOT\Folder\shell\webists_serverpathtoclip\command\"
    ' wshShell.RegDelete "HKEY_CLASSES_ROOT\Folder\shell\webists_serverpathtoclip\"

    On Error Goto 0

    Set wshShell = Nothing
    WScript.Echo "Script successfully unregistered."
    WScript.Quit 0
End Sub


Sub Syntax
    Dim strMsg
    strMsg = "Webists_GetCorrespondingServerPath.vbs,  Version 1.00" & vbCrLf _
           & "written by Andy Brennenstuhl @ The Webists" & vbCrLf _
           & "http://www.thewebists.com" & vbCrLf & vbCrLf _           
           & "Use this script to get corresponding server paths of synchronised files." & vbCrLf & vbCrLf _
           & "MAKE SURE TO CONFIGURE BY EDITING CONST VALUES IN THE SCRIPT." & vbCrLf & vbCrLf _
           & "Usage:  WSCRIPT  Webists_GetCorrespondingServerPath.vbs  ""text string"" | /Register | /Unregister" & vbCrLf & vbCrLf _
           & "Where:  ""text string""   is the full local path of the files you want to get the URL for" & vbCrLf _
           & "  /Register   Adds an entry ""Copy Webists Viewer Path"" to Explorers' context menu" & vbCrLf _
           & "  /UnRegister   Removes the menu entry again" _
           & vbCrLf & vbCrLf _
           & "Based on 'SendClip.vbs' Written by Rob van der Woude" & vbCrLf _
           & "http://www.robvanderwoude.com"
    WScript.Echo strMsg
    WScript.Quit 1
End Sub

我唯一不喜欢的是它使用IE将字符串放入剪贴板的方式,因为它每次都要求权限。

有人能提出更好的方法吗?

Sub SendToClipboard(strToClipboard)
    Set objIE = CreateObject( "InternetExplorer.Application" )
    objIE.Navigate( "about:blank" )
    objIE.Document.ParentWindow.ClipboardData.SetData "text", strToClipboard
    objIE.Quit
    Set objIE = Nothing
End Sub 

答案 2 :(得分:0)

很抱歉,这是法语,但如果您有兴趣,我可以假设某些部分的翻译。我在一年前写了一篇名为Un menu contextuel "Full PowerShell" dans l'explorer de Windows的文章,可以将其翻译为“Windows资源管理器中的完整PowerShell上下文菜单”。给出的具体示例是文件的MD5哈希计算。

答案 3 :(得分:0)

根本不需要使用任何脚本

设置密钥的默认值 HKEY_CLASSES_ROOT * \ shell \ Copy Server Path \ Command 至: cmd.exe / c echo&#34;%1&#34; | clip

完成工作...... 祝你今天愉快 PS我从http://www.askvg.com/registry-tweak-to-add-copy-as-path-option-in-files-and-folders-context-menu-in-windows/

得到了这个
相关问题