Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select file name in windows explorer

Status
Not open for further replies.

PMGSI

IS-IT--Management
Dec 18, 2015
4
GB
Hi there

Could someone tell me please if this is possible in vbs:

I'd like to be able to select a filename from a windows explorer window. For example, file 3 is selected below:

sample%20file%20list_zpsofvyg1qr.jpg


Ultimately I'd like to be able to have the file in 'edit mode' - ie the same state as if you'd right-clicked on it and selected rename, but I can do this by passing an F2 keypress, so all I really need is a command to select the file I ask it to.

Is this do-able at all?
 
See Link.
basically, you just launch explorer.exe with the /select switch:
e.g.,
Code:
Dim sh
Set sh = CreateObject("WScript.Shell")
Call sh.Run("explorer.exe /select,""C:\Users\justin\Desktop\file 3.txt""")
 
Thanks for replying. Unfortunately not quite what I wanted, though it's pretty close.

What I'd like to do is select the filename in the currently open explorer window without having to open a new one. It seems like this should be possible from the link you kindly provided, but I haven't managed it yet!

What I've got so far is your code plus a call to get the current directory. Whitespace.bmp is just the test file I'm trying to select:

Code:
Dim sh
Set sh = CreateObject("WScript.Shell")
Call sh.Run("explorer.exe /select," & sh.currentdirectory & """\whitespace.bmp""")

 
Something like the following:
Code:
[blue]If OpenForRename("whitespace.bmp") = False Then MsgBox "Unable to find file in Explorer window"

[green]' OpenForRename selects the nominated file, strFileName, in the most recently opened Explorer window ready for the name to be edited
' Opening the Explorer windows itself is not included in this example[/green]
Private Function OpenForRename(strFilename)
    Dim sh
   
    Set sh = CreateObject("shell.application") [green]' New Shell[/green]

    On Error Resume Next [green]' some fairly evil, rudimentary error handling ...[/green]
    With sh.Windows(sh.Windows.Count - 1).document
        .SelectItem .Folder.Items.Item("test.txt"), 17 [green]' Select the item we want to act on[/green]
        .Folder.Items.Item(strFilename).InvokeVerb "Rename" [green]' Invoke 'rename' on selected file[/green]
        If Err <> 0 Then [green]' filename is not in Explorer window[/green]
            OpenForRename = False
            Exit Function
        End If
        OpenForRename = True [green]' if we get here then we have been successful[/green]
    End With
End Function[/blue]
 
Hmm - can't seem to get that to work but it looks like what I'm after.

I'll go over it and see if I can find out what's happening.

Cheers
 
Oh, simple cut'n'paste error on my behalf, sorry; I failed to update the function for generic filename.

Change

[tt].SelectItem .Folder.Items.Item("test.txt"), 17[/tt]

to

[tt].SelectItem .Folder.Items.Item(strFilename), 17[/tt]
 
Thanks - I did notice that (should have mentioned it in my last post!) but it's still not working.

It doesn't find the file at the start of the script, so triggers the 'unable to find' message box. I thought this is just related to knowing what the current location is, so I'd look at it later and rem'd it out.

The rest of the script executes without complaint, but doesn't seem to select the file as it should (even with resume on error disabled).

I'll have a look through it again when I get a min - unfortunately I've got stuck with a few other work priorities so I'm only able to get back to this between other stuff!

Cheers


EDIT - Success! - it was just me being silly as I didn't realise the function wouldn't be called at all when I rem'd out the line:

Code:
If OpenForRename("whitespace.bmp") = False Then MsgBox "Unable to find file in Explorer window"

I've put it back in and now all is well.

Many thanks for your guidance on this - much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top