2

私が求めている要件は非常に単純ですが、この件に関する膨大な量の無関係な情報をふるいにかけるのに苦労しています。

要件

PC と Web サーバーの間でファイルを同期しています。基本的に、ローカル ファイルを右クリックして、同等のサーバー パスを (追加の文字列操作ロジックと共に) クリップボードにコピーできるようにする必要があります。

アプローチ

私がする必要があるのは次のことだと思います:

  1. Windows エクスプローラーのコンテキスト メニュー オプションを追加して、完全なファイル パスと名前をパラメーターとして渡す WSH スクリプトを実行します。
  2. そのパラメーターを受け入れ、必要な文字列操作を行い、クリップ ボードにコピーする Windows Scripting Host スクリプトを作成します。

文字列操作は問題なく処理できます (できれば VBScript で)。実際に情報を見つけることができないのは、パラメーターを WSH スクリプトに渡す全体です。

または、これが PowerShell で行われた場合 (該当する場合) も気にしないので、作業中にもう少し詳しく学ぶことができます。

よろしくお願いします。

4

4 に答える 4

1

次のようにして、VB スクリプトのパラメーターにアクセスできます。

WScript.Echo(WScript.Arguments(0))

シェルのコンテキスト メニュー コマンドを登録すると、次のように登録することで、パスとファイル名をパラメーターとして渡すことができます。

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

申し訳ありませんがフランス語ですが、興味があれば一部の翻訳を想定させていただきます。私は1年前に、「Windowsエクスプローラーの完全なPowerShellコンテキストメニュー」と翻訳できるUn menu contextuel "Full PowerShell" dans l'explorerdeWindowsという記事を書きました。具体的なサンプルは、ファイルのMD5ハッシュコンピューティングです。

于 2012-02-07T20:51:22.093 に答える