DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: VBS: Select one file in a folder  (Read 6,797 times)

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22
VBS: Select one file in a folder
« on: July 18, 2017, 03:48 PM »
Hello,

The following code selects arg1 in a Windows Explorer folder:

Code
Set objShell = WScript.CreateObject("WScript.Shell")
	objShell.Run("""" & WScript.Arguments(0) & """")
	WScript.Sleep 400

	' objShell.SendKeys("^a{F5}")		' Deslect All.

Set objShellAPP = CreateObject("Shell.Application")
	On Error Resume Next		' For new unsaved files.
	With objShellAPP.Windows(objShellAPP.Windows.Count - 1).document
	.SelectItem .Folder.Items.Item(WScript.Arguments(1)), 17
	End With

Set objShell = Nothing
Set objShellAPP = Nothing

If fileA is already selected in the folder and fileB is passed as arg1, both files are selected.

How can I have fileA deselected and only fileB selected?
The commented line
Code
objShell.SendKeys("^a{F5}")
is a workaround, but there must be a better way.

Thank you.

Yaron

  • Participant
  • Joined in 2014
  • *
  • Posts: 22

Original address: https://www.donationcoder.com/forum/index.php?topic=44105.0