1

The following code selects arg1 in a Windows Explorer folder:

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 objShell.SendKeys("^a{F5}")is a workaround, but there must be a better way.

Thank you.

1
  • Maybe the following vbs code can help objWsShell.Run("explorer.exe /select," & itmToSelect): it runs an instance of explorer.exe to browse local folders. To the instance is passed the switch /select and a parameter itmToSelect that is the path (as string) to a file or folder to select. After the execution of the code you will have the itmToSelect (file or folder) highlighted into the file/folder browser. Commented Aug 9, 2023 at 20:42

1 Answer 1

2

You pass the value of 17 for the parameter dwFlags. This value is an integer representing the combination of flags you want set.

You derive this value by a bitwise OR operation using the flag values. I think 17 is probably 16 OR 1 (give the item focus; select item) There is a flag value of 4 to "deselect all but the specified item." 17 OR 4 = 21.

Use OR to combine flags; use AND to test against a particular flag to see if it is set (21 AND 4 = 4); use AND not to toggle one flag off (21 AND not(4) = 17)

SelectItem Documentation

random link: Stackoverflow - How do integer flags work?

3
  • Note: flags make more sense if you consider them in binary and then consider that binary is analogous with a bank of toggle switches. So "8" is 1000 or On-Off-Off-Off.
    – Yorik
    Commented Jul 18, 2017 at 21:55
  • Perfect. Thank you very much. 21 works like a charm.
    – Yaron
    Commented Jul 18, 2017 at 23:09
  • superuser.com/questions/1233283/…
    – Yaron
    Commented Jul 22, 2017 at 5:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .