×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Select file name in windows explorer

Select file name in windows explorer

Select file name in windows explorer

(OP)
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:



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?

RE: Select file name in windows explorer

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""") 

RE: Select file name in windows explorer

(OP)
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""") 

RE: Select file name in windows explorer

Something like the following:

CODE

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

' 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
Private Function OpenForRename(strFilename)
    Dim sh
   
    Set sh = CreateObject("shell.application") ' New Shell

    On Error Resume Next ' some fairly evil, rudimentary error handling ...
    With sh.Windows(sh.Windows.Count - 1).document
        .SelectItem .Folder.Items.Item("test.txt"), 17 ' Select the item we want to act on
        .Folder.Items.Item(strFilename).InvokeVerb "Rename" ' Invoke 'rename' on selected file
        If Err <> 0 Then ' filename is not in Explorer window
            OpenForRename = False
            Exit Function
        End If
        OpenForRename = True ' if we get here then we have been successful
    End With
End Function 

RE: Select file name in windows explorer

(OP)
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

RE: Select file name in windows explorer

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

Change

.SelectItem .Folder.Items.Item("test.txt"), 17

to

.SelectItem .Folder.Items.Item(strFilename), 17

RE: Select file name in windows explorer

(OP)
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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close