0

I have tried getting the file path from Clipboard (copied file) in Windows Explorer.

I wrote:

Set Ws = CreateObject("WScript.Shell")
MsgBox Ws.ClipBoard

But it states error:

Error: Object doesn't support this property or method: 'Ws.ClipBoard'

I got a number of methods for getting the clipboard data but only related to the internet explorer, and not for the windows explorer file path related.

Udhav Sarvaiya
  • 9,380
  • 13
  • 53
  • 64
Thompson
  • 1,954
  • 10
  • 33
  • 58
  • I have already seen that question and answer before posting this question. That uses Internet explorer method. But I want to get windows copied file file path (in clipboard) – Thompson Mar 23 '19 at 21:04
  • https://pastebin.com/tm7pbLzm this uses VB.NET as VBScript has no clipboard functions (it needs to find an application with clipboard functions and use the app's). Copied filenames are in HDrop format and most apps can't handle that format. – Noodles Mar 23 '19 at 21:04
  • `WScript.Shell` documentation does NOT say there is a clipboard function. So we **know** it can't work. It takes 2 minutes to read through all shell scripting functions. – Noodles Mar 23 '19 at 21:06
  • @Thompson yes it does, which is the workaround as [tag:wscript] doesn't have clipboard functionality built into it. The duplicate shows how to use the IE clipboard to store whatever you want, it's up to you to grab the file path and put it in the clipboard. – user692942 Mar 25 '19 at 12:56
  • Possible duplicate of [Use clipboard from VBScript](//stackoverflow.com/q/128463) – user692942 Mar 25 '19 at 13:00
  • @Lankymart The linked duplicate **does not** answer or attempt to answer the question which is *how to get file names from the clipboard*. The linked answer talks about getting **text** from the clipboard and the techniques shown **cannot** get filenames. – Noodles Mar 25 '19 at 20:35

1 Answers1

0

This shows how to work with HDrop. VBScript is easily ported to VB.NET (enclose all parameters in brackets and dim everything As Object).

REM PrintClip.bat #2 Also does filenames on the clipboard
REM This file compiles PrintClip.vb to PrintClip.exe
REM PrintClip.exe prints any text or filenames on the clipboard to a console
REM To use 
REM PrintClip
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\PrintClip.exe" "%~dp0\PrintClip.vb" /verbose
pause

'PrintClip.vb #2
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module PrintClip
    Public Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Integer) As Integer
    Public Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Integer) As IntPtr
    Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As IntPtr) As Integer
    Public Declare Function CloseClipboard Lib "user32" () As Integer
    Public Declare UNICODE Function DragQueryFileW Lib "shell32.dll" (ByVal HDROP As Integer, ByVal UINT As Integer, ByVal lpStr As String, ByVal ch As Integer) As Integer

    Public Const CF_TEXT = 1
    Public Const CF_BITMAP = 2
    Public Const CF_METAFILEPICT = 3
    Public Const CF_SYLK = 4
    Public Const CF_DIF = 5
    Public Const CF_TIFF = 6
    Public Const CF_OEMTEXT = 7
    Public Const CF_DIB = 8
    Public Const CF_PALETTE = 9
    Public Const CF_PENDATA = 10
    Public Const CF_RIFF = 11
    Public Const CF_WAVE = 12
    Public Const CF_UNICODETEXT = 13
    Public Const CF_ENHMETAFILE = 14
    Public Const CF_HDROP = 15
    Public Const CF_OWNERDISPLAY = &H80
    Public Const CF_DSPTEXT = &H81
    Public Const CF_DSPBITMAP = &H82
    Public Const CF_DSPMETAFILEPICT = &H83
    Public Const CF_DSPENHMETAFILE = &H8E


    Sub Main()
'       On Error Resume Next
        Dim Ret as IntPtr
        If OpenClipboard(0) <> 0 then
            If IsClipboardFormatAvailable(CF_UNICODETEXT) <> 0 then
                Ret = GetClipboardData( CF_UNICODETEXT)
                Console.writeline(Marshal.PtrToStringUni(Ret))
                Environment.ExitCode = 0
            ElseIf IsClipboardFormatAvailable(CF_hDrop) <> 0 then
                Dim TotalCount as Integer
                Dim FName as String
                Dim hDrop as IntPtr
                hDrop = GetClipboardData( CF_hDrop)
                FName = Space(33000)
                TotalCount = DragQueryFileW(hDrop,  &hFFFFFFFF, FName, 33000)
                For x = 0 to TotalCount - 1
                    FName = Space(33000)
                    If DragQueryFileW(hDrop,  x, FName, 33000) <> 0 then
                        Console.writeline(Trim(FName))
                    End If
                Next
                Environment.ExitCode = 0
            Else
                Environment.ExitCode = 1
                Console.writeline("No text or filenames on clipboard")
            End If
            CloseClipboard()
        Else
            Environment.ExitCode = err.lastdllerror
            Console.Writeline("Clipboard is locked by another application")
        End If

    End Sub
End Module
Noodles
  • 194
  • 1
  • 4