使用资源管理器上下文菜单将文件路径的修改版本复制到剪贴板

发布于 2025-01-03 08:37:29 字数 478 浏览 1 评论 0 原文

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

要求

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

方法

我想我需要做的事情如下:

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

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

或者,我不介意是否使用 PowerShell(如果适用)完成此操作,以便我可以在操作过程中了解更多信息。

非常感谢。

The requirement I've got is pretty straightforward, but I'm struggling sifting through the huge amounts of unrelated information on the subject.

Requirement

I have files synchronised between my PC and my web server. Basically I need to be able to right click on a local file and copy the equivalent server path (with some additional string manipulation logic) to the clipboard.

Approach

I guess what I need done is the following:

  1. Add a Windows Explorer context menu option to execute a WSH script passing the full file path and name as a parameter.
  2. Create a Windows Scripting Host script that will accept that paramater, do necessary string manipulation, and copy to clip board.

The string manipulation I can handle fine (preferably in VBScript). It's the whole passing a parameter to the WSH script that I can't really find any info about.

Alternately, I wouldn't mind if this was done with PowerShell (if applicable) so that I can learn a bit more about it while I'm at it.

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再浓的妆也掩不了殇 2025-01-10 08:37:29

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

WScript.Echo(WScript.Arguments(0))

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

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

You can access a parameter in VB script thus:

WScript.Echo(WScript.Arguments(0))

When you register a shell context menu command, you can pass the path and filename as a parameter by registering:

wscript.exe "C:\...full path...\myscript.vbs" "%1"
赴月观长安 2025-01-10 08:37:29

好的,我修改了一个 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 

Ok, I've modified a VBS script by that registers itself in the Explorer context menu and the allows you to right click on a file to copy it's corresponding server URL to the clipboard.

'####################################################################
' 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

The only thing I don't like is the bit the way it uses IE to put the string in the clipboard since it asks for permission every time.

Can anyone suggest a better approach?

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 
小红帽 2025-01-10 08:37:29

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

Sorry it's in french, but I can assume the translation of some parts if you are interested. I wrote a year ago an article called Un menu contextuel "Full PowerShell" dans l'explorer de Windows which can be translated as "a full powershell contextual menu in Windows Explorer". The concrete given sample is the MD5 hash computing of a file.

枕头说它不想醒 2025-01-10 08:37:29

根本不需要使用任何脚本

设置 key 的默认值
HKEY_CLASSES_ROOT*\shell\复制服务器路径\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-menu-in-windows/

No need to use any script at all

set the Default value of key
HKEY_CLASSES_ROOT*\shell\Copy Server Path\Command
to:
cmd.exe /c echo "%1"|clip

Job done...
Have a nice day
PS I got this from http://www.askvg.com/registry-tweak-to-add-copy-as-path-option-in-files-and-folders-context-menu-in-windows/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文