The Path to File Enlightenment

One of the most useful little scripts I’ve written allows me to copy a file’s path to the clipboard.  This provides a quick and easy way of pasting it into a file attachment dialog or even into the body of an e-mail. It’s certainly one of the most useful little automations I use on a daily basis.

The script takes the file path as its argument, which may seem quite pointless until you realise that you can drag and drop a file onto the script, or even simply add a shortcut to the script onto your send-to menu.

The script has two modes - one for the pure file path and one for automatically converting this into a URL.  This is controlled by setting the intMode variable to one of the constants.  The example below has it set to MODE_BASIC which is the straight forward copy.

Option Explicit

Const MODE_BASIC = 1
Const MODE_URL = 2

Dim objArgs
Dim strFilename
Dim strContent
Dim intMode

intMode = MODE_BASIC

'Get the arguments for the script
Set objArgs = Wscript.Arguments
If objArgs.Count > 0 Then
    If objArgs.Count = 1 Then
        Select Case intMode
            Case MODE_BASIC
                sFilename = Wscript.Arguments(0)
            Case MODE_URL
                strFilename = ConvertFilenameToURL(Wscript.Arguments(0))
            Case Else
                Msgbox "The translation mode set for this script is invalid.", vbOkOnly + vbCritical, "Invalid Mode"
        End Select
        Msgbox "Please pass one file at a time to this script", vbOkOnly + vbExclamation, "Too Many Files"
    End If
    Msgbox "Please pass a file to this script", vbOkOnly + vbExclamation, "No File Provided"
End If


MsgBox strFilename, vbOkOnly + vbInformation, CStr(Len(strFilename)) + " Characters Copied to clipboard"


Function CopyPaste(p_strCopyText)
    Dim objDummy

    With CreateObject("Scripting.FileSystemObject")
        With .CreateTextFile("Child.vbs")
            .WriteLine "CreateObject(""WScript.Shell"").SendKeys ""^c{ENTER}"""
        End With

        CreateObject("WScript.Shell").Run "Child.vbs"
        objDummy = InputBox("","" , p_strCopyText, 20000, 20000)
        .DeleteFile "Child.vbs"
    End With
End Function

Function ConvertFilenameToURL(p_strFilename)
    ConvertFilenameToURL = "file://" + Replace(p_strFilename, "\", "/")
    ConvertFilenameToURL = Replace(ConvertFilenameToURL, " ", "%20")
End Function

I have two copies of this script with each with the opposite mode set and two shortcuts in my send-to menu meaning I always have both options available to me.

You may wish to streamline this further by parameterising the mode or passing the file path result to another script to convert it to a URL in order to make your collection of scripts more efficient.  By all means do, this was just the quickest way for me to put the scripts I have together to share with everyone else.

Author: Stephen Millard
Tags: | vbs |

Buy me a coffeeBuy me a coffee

Related posts that you may also like to read