2012-02-07 152 views
2

我有要求是非常简单的,但我挣扎过大量的关于这个主题无关的信息进行筛选。使用资源管理器右键菜单中的文件路径的修改版本复制到剪贴板

要求

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

方法

我想我需要做的是以下几点:

  1. 添加Windows资源管理器上下文菜单选项来执行WSH脚本传递完整的文件路径和名称作为参数。
  2. 创建一个Windows脚本宿主脚本,将接受paramater,做必要的字符串操作,并复制到剪贴板。

的字符串操作我可以处理细(最好在VBScript)。这是整个传递给WSH脚本的参数,我无法真正找到任何有关信息。

或者,如果这是使用PowerShell完成(如适用),这样我可以学到更多的关于它的,而我在这我不会介意。

非常感谢提前。

+0

我已经回答以下问题。 – AndyAtTheWebists 2012-02-08 12:00:52

回答

1

可以在VB脚本从而访问参数:

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

这并不完美。我用下面的,现在它的罚款:他们在此注册表项默认值 'WScript.exe的 “[FULLPATH] \ myscript.vbs” “%1”' 添加了这个: 'HKEY_CLASSES_ROOT \ * \壳\复制服务器路径\命令' 现在只需编写vbs脚本(我会在后面为其他任何可能需要的人发布它)。 – AndyAtTheWebists 2012-02-07 20:27:31

+0

对不起,我更新了你的更正。 – arx 2012-02-07 21:38:24

0

WScript.Echo(WScript.Arguments(0)) 

当您注册一个外壳上下文菜单命令,您可以通过注册传递路径和文件名作为参数对不起,这是法文,但如果你有兴趣,我可以假设一些部分的翻译。我在一年前写了一篇名为Un menu contextuel "Full PowerShell" dans l'explorer de Windows的文章,可以将其翻译为“Windows资源管理器中的完整PowerShell上下文菜单”。具体给出的示例是文件的MD5哈希计算。

+0

谢谢,但我已经开始使用WSH开发解决方案。 – AndyAtTheWebists 2012-02-07 22:15:49

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 
相关问题