Quick file access script

Discussion in 'Scripting' started by badsyntax32, Jun 25, 2013.

  1. badsyntax32

    badsyntax32 MDL Novice

    Jun 25, 2013
    8
    7
    0
    #1 badsyntax32, Jun 25, 2013
    Last edited by a moderator: Apr 20, 2017
    Just a quick share, I always found this small VB script extremely handy to access files on my flash drive. It only requires a decent memory (mentally) to partially remember file names and a love for your keyboard is also applicable.
    Code:
    If WScript.Arguments.Count = 0 Then WScript.Quit
    If Len(WScript.Arguments(0)) = 0 Then WScript.Quit
    
    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    SearchPath = "F:\"
    SearchStr = LCase(WScript.Arguments.Item(0))
    If InStr(SearchStr, ":") Then 
      SearchName = Mid(SearchStr, InStr(SearchStr, ":") + 1)
      SearchProp = Mid(SearchStr, 1, InStr(SearchStr, ":") - 1)
    Else
      SearchName = SearchStr
      SearchProp = ""
    End If
    SearchAttr = 0
    If WScript.Arguments.Count >= 2 Then
      SearchAttr2 = LCase(WScript.Arguments.Item(1))
      If InStr(SearchAttr2, "r") Then SearchAttr = (SearchAttr OR 1)
      If InStr(SearchAttr2, "h") Then SearchAttr = (SearchAttr OR 2)
      If InStr(SearchAttr2, "s") Then SearchAttr = (SearchAttr OR 4)
      If InStr(SearchAttr2, "d") Then SearchAttr = (SearchAttr OR 16)
    Else
      SearchAttr2 = ""
    End If
    Set Found = CreateObject("System.Collections.ArrayList")
    
    If objFSO.FolderExists(SearchPath) Then
      Find objFSO.GetFolder(SearchPath), Found
      If Found.Count > 0 Then
        If Found.Count = 1 Then
          Run Found.Item(0)
        Else
          For i = 0 To Found.Count - 1
            WScript.Echo "[" & (i + 1) & "]" & " " & Mid(Found.Item(i), IIf(Right(SearchPath, 1) = "\", Len(SearchPath), Len(SearchPath) + 1))
          Next
          WScript.Echo ""
          WScript.StdOut.WriteLine ("Select" & " " & "[" & Found.Count & "]" & " " & ":")
          strPrompt = WScript.StdIn.ReadLine
          If IsNumeric(strPrompt) Then
            Prompt = CInt(strPrompt)
            If Prompt <= Found.Count Then
              Run Found.Item(Prompt - 1)
            End If
          End If
        End If
      Else
        MsgBox "No objects found", vbInformation
      End If
    End If
    
    Sub Run(strCommand)
      strCommand = Chr(34) & strCommand & Chr(34)
      If InStr(SearchProp, "n") And (SearchAttr AND 16) = 0 Then strCommand = "notepad" & " " & strCommand
      objShell.Run strCommand, 1, 0
    End Sub
    
    Sub Find(objFolder, ByRef Found)
      If (SearchAttr AND 16) = 0 Then
        For Each objFile In objFolder.Files
          If IsMatch(objFile.Name) Then
            If (objFile.Attributes AND SearchAttr) = SearchAttr Then
              Found.Add(objFile.Path)
            End If
          End If
        Next
      End If
      For Each objFolder2 In objFolder.SubFolders
        If (SearchAttr AND 16) Then
            If IsMatch(objFolder2.Name) Then
              If (objFolder2.Attributes AND SearchAttr) = SearchAttr Then
                Found.Add(objFolder2.Path)
              End If
            End If
        End If
        Find objFolder2, Found
      Next
    End Sub
    
    Function IsMatch(Name)
      If InStr(SearchProp, "x") Then
        If LCase(Name) = SearchName Then IsMatch = True
      Else
        If InStr(LCase(Name), SearchName) Then IsMatch = True
      End If
    End Function
    
    Function IIf(Expression, TruePart, FalsePart)
      If Expression = True Then
        If IsObject(TruePart) Then Set IIf = TruePart Else IIf = TruePart
      Else
        If IsObject(FalsePart) Then Set IIf = FalsePart Else IIf = FalsePart
      End If
    End Function
    
    vbulletin messed up the formatting, you can optionally see the code here: pastebin.com/G34A6kT8
    It basically works like this:
    You add the folder path containing the script file to the PATH variable. Next, relogon and run the script with a parameter from the run box.
    for example:
    go.vbsc auto
    Note that you will be typing the file extension because the windows shell doesn't automatically recognize VBS files if no extension has been specified. To solve this; create a shortcut in the same folder as the script and name to whatever you like. 'go myfile' will work now assuming that the shortcut is named 'go.lnk'

    Typing the full name is unnecessary, if the script matches more than one file then it will prompt for a selection.
    For an exact match, use the following syntax:
    go
    x:myfile
    To open the found file with notepad use:
    go
    n:myfile
    To match certain attributes, use a second argument:
    go myfile
    rhs
    will match read-only, hidden and system files
    To match a folder
    use:
    go myfolder d

    Important:
    It has to run in a command line environment. I advice to use a special 'vbsc' extension for command-line
    scripts.
    You can use this registry script if you wish:
    Code:
    Windows Registry Editor Version 5.00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile]
    "FriendlyTypeName"=hex(2):40,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,\
      00,6f,00,6f,00,74,00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,\
      32,00,5c,00,77,00,73,00,68,00,65,00,78,00,74,00,2e,00,64,00,6c,00,6c,00,2c,\
      00,2d,00,34,00,38,00,30,00,32,00,00,00
    @="VBScript Script File"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\DefaultIcon]
    @=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\
      00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,53,00,\
      63,00,72,00,69,00,70,00,74,00,2e,00,65,00,78,00,65,00,2c,00,32,00,00,00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ScriptEngine]
    @="VBScript"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ScriptHostEncode]
    @="{85131631-480C-11D2-B1F9-00C04F86C324}"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\Shell]
    @="Open"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\Shell\Edit]
    @="&Edit"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\Shell\Edit\Command]
    @=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\
      00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,4e,00,6f,00,\
      74,00,65,00,70,00,61,00,64,00,2e,00,65,00,78,00,65,00,20,00,25,00,31,00,00,\
      00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\Shell\Open]
    @="&Open"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\Shell\Open\Command]
    @=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,\
      00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,43,00,53,00,\
      63,00,72,00,69,00,70,00,74,00,2e,00,65,00,78,00,65,00,20,00,22,00,25,00,31,\
      00,22,00,20,00,25,00,2a,00,00,00
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ShellEx]
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ShellEx\DropHandler]
    @="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ShellEx\PropertySheetHandlers]
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSCFile\ShellEx\PropertySheetHandlers\WSHProps]
    @="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
    
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.vbsc]
    @="VBSCFile"
    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.vbsc\PersistentHandler]
    @="{5e941d80-bf96-11cd-b579-08002b30bfeb}"
    
    And ofcourse, to set the folder to search in on each execution, change the SearchPath variable.


    vbulletin messed up the formatting of this entire post, sorry for that..