2

我的要求非常简单,但我正在努力筛选关于该主题的大量不相关信息。

要求

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

方法

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

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

我可以很好地处理字符串操作(最好在 VBScript 中)。将参数传递给我找不到任何信息的 WSH 脚本的整个过程。

或者,我不介意这是否是使用 PowerShell(如果适用)完成的,这样我可以在使用它的同时了解更多关于它的信息。

提前谢谢了。

4

4 回答 4

1

您可以这样访问 VB 脚本中的参数:

WScript.Echo(WScript.Arguments(0))

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

wscript.exe "C:\...full path...\myscript.vbs" "%1"
于 2012-02-07T19:19:56.423 回答
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 
于 2012-02-08T11:59:17.070 回答
0

对不起,它是法语,但如果你有兴趣,我可以翻译一些部分。一年前我写了一篇名为Un menu contextuel "Full PowerShell" dans l'explorer de Windows的文章,可以翻译为“Windows Explorer 中的完整 powershell 上下文菜单”。具体给出的示例是文件的 MD5 哈希计算。

于 2012-02-07T20:51:22.093 回答
0

根本不需要使用任何脚本

将键 HKEY_CLASSES_ROOT*\shell\Copy Server Path\Command 的默认值设置为: cmd.exe /c echo "%1"|clip

工作完成...祝你有美好的一天 PS 我从http://www.askvg.com/registry-tweak-to-add-copy-as-path-option-in-files-and-folders-context-得到这个窗口中的菜单/

于 2015-01-28T00:16:56.933 回答