VBS: Select one file in a folder
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.
Solution 1:
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?