Auto reload ListView when add or remove drive Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Auto reload ListView when add or remove drive

02 Sep 2020, 16:04

A tiny script to show attached drives including volume label and free space:

Code: Select all

#SingleInstance Force
#Persistent
#NoTrayIcon
#NoEnv
SendMode Input
SetBatchLines -1
SetWinDelay, 0

Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,font,s10,Tahoma
Gui,Margin,0,0
Gui,Add,ListView, Grid R9 -HDR,Drive|Label|Free

DriveGet,list,List
Loop,Parse,list,
{
  drive:=A_LoopField ":"
  Gosub,DriveGet
  LV_Add("",drive,label,free)
  LV_ModifyCol()
}

Gui,Show,,Active Drives
Return

DriveGet:
DriveGet,fs,Fs,%drive%\
DriveGet,label,Label,%drive%\
DriveSpaceFree,free,%drive%\
Return

GuiClose:
ExitApp
When I add or remove a drive, I would like the script to auto reload to show the change. I've tried using SetTimer and LV_GetCount() - when the row count changes, reload the script - without success. Can someone point me in the right direction?
User avatar
mikeyww
Posts: 1081
Joined: 09 Sep 2014, 18:38

Re: Auto reload ListView when add or remove drive

03 Sep 2020, 20:29

What does "without success" mean? What problem are you facing?
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Re: Auto reload ListView when add or remove drive

04 Sep 2020, 14:45

Thanks for responding. I was searching for a method to determine when an external drive was inserted or removed from my computer so that my ListView app, which shows the attached drives, could auto-reload to reflect the change. I was using LV_GetCount() to get the initial count of drives and then SetTimer to determine when the count changed. In all events, I couldn't get this method to work. However, I found the OnMessage method that does exactly what I want:

Code: Select all

OnMessage(0x219, "notify_change")
Return
notify_change(wParam, lParam, msg, hwnd)
{ 
reload
}
Return
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: Auto reload ListView when add or remove drive

04 Sep 2020, 15:40

@Bill , thank you for notify_change
here an idea without reload

Code: Select all

;- Auto reload ListView when add or remove drive - AutoHotkey Community - Mozilla Firefox
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80592

#Warn
#SingleInstance Force
#Persistent
#NoTrayIcon
#NoEnv
SendMode Input
SetBatchLines -1
SetWinDelay, 0
Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,font,s10,Tahoma
Gui,Margin,0,0
Gui,Add,ListView, Grid R9 -HDR vLV1,Drive|Label|Free
Gui,Show,,Active Drives
gosub,drivegetshow
OnMessage(0x219, "notify_change")
Return
;---------------
GuiClose:
ExitApp
;---------------
notify_change(wParam, lParam, msg, hwnd)
{ 
gosub,drivegetshow
}
Return
;---------------
drivegetshow:
sleep,500
i=0
alldrives:=""
LV_Delete()
DriveGet, A, List,
StringSplit,D, A,
GuiControl,1: -Redraw,LV1
loop,%d0%
{
DRV:= % d%a_index% . ":\"
i++
      DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
      DriveGet       , cap1    , capacity , %DRV%
      DrivespaceFree , free1   ,            %DRV%
      DriveGet       , fs1     , fs       , %DRV%     ;- FAT
      DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
      DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
      DriveGet       , status1 , status   , %DRV%
alldrives .= drv . "  " . type1 . "  " . free1 . "-MB`n"
LV_Add("",drv,label1,free1)
}
LV_ModifyCol()
GuiControl,1: +Redraw,LV1
return
;============================================
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Re: Auto reload ListView when add or remove drive

05 Sep 2020, 02:34

Wow. Thanks Garry. Much more elegant. I'm going to have to spend some time to try to understand the code!
User avatar
mikeyww
Posts: 1081
Joined: 09 Sep 2014, 18:38

Re: Auto reload ListView when add or remove drive

05 Sep 2020, 08:10

That's a very nice script. To show column headers, I remove "-HDR" from the ListView.

Code: Select all

Gui, Add, ListView, Checked Grid R18 vLV1, Drive|Label|GB free|`% free
Gui, Show,, Active Drives (ESC = Exit)
Could also then update LV_Add and column modifiers.

Code: Select all

pct := free1 ? Round(100*free1/cap1) : "", ok := pct > 19 || fs1 != "NTFS" ? "Check" : ""
LV_Add(ok, Format("{:5}", drv), label1, free1 ? Floor(free1/1024) : "", pct)

Code: Select all

LV_ModifyCol(1, 65)
LV_ModifyCol(2)
LV_ModifyCol(3, "55 Right")
LV_ModifyCol(4, "65 Center")
This will format the columns nicely. It also adds a space check: marks rows with free space of at least 20%.

Can also add a notification message inside the notify_change function.

Code: Select all

MsgBox, 262208, Drives changed, Drives have changed.
Press ESC to exit.

Code: Select all

#IfWinActive Active Drives ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe
ESC::ExitApp
#IfWinActive
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Re: Auto reload ListView when add or remove drive  Topic is solved

05 Sep 2020, 12:56

Great ideas. Trying them all. I made a few small changes. I removed Checked, added a separate column for GB and a width to the listview.

Code: Select all

Gui, Add, ListView, Grid R18 vLV1 w460, Drive|Label|MB free|GB free|`% free
The GB and percentage info is excellent! Thanks for this. I removed the qualification to indicate rows with free space of at least 20%. I also used RegEx to insert commas into the MB display.

Code: Select all

pct := free1 ? Round(100*free1/cap1) : ""
free2 := RegExReplace(free1, "(\G|[^\d.])\d{1,3}(?=(\d{3})+(\D|$))", "$0,")
LV_Add("", Format("{:5}", drv), label1, free2, free1 ? Floor(free1/1024) : "", pct)
}
LV_ModifyCol(1, 45)
LV_ModifyCol(2)
LV_ModifyCol(3, "65 Right")
LV_ModifyCol(4, "65 Right")
LV_ModifyCol(5, "65 Center")
What does "Format("{:5}" do?
User avatar
mikeyww
Posts: 1081
Joined: 09 Sep 2014, 18:38

Re: Auto reload ListView when add or remove drive

05 Sep 2020, 14:05

https://www.autohotkey.com/docs/commands/Format.htm

This is a better exit routine, for the bottom of the script.

Code: Select all

GuiClose:
GuiEscape:
ExitApp
BoBo
Posts: 3761
Joined: 13 May 2014, 17:15

Re: Auto reload ListView when add or remove drive

05 Sep 2020, 14:37

What does "Format("{:5}" do?
Adding 5 space-characters to the left of the content of the variable.

Code: Select all

MsgBox % "|" Format("{:5}", 1) "|"	; output: |     1|
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Re: Auto reload ListView when add or remove drive

05 Sep 2020, 15:18

Thanks, all, for the suggestions and great support!
garry
Posts: 2433
Joined: 22 Dec 2013, 12:50

Re: Auto reload ListView when add or remove drive

06 Sep 2020, 13:20

see also DriveGetOb() from user BoBo
https://www.autohotkey.com/boards/viewtopic.php?f=10&t=80688

idea , if 'CLOSE' the GUI it will be minimized, see GUI again if drives have changed

Code: Select all

;- Auto reload ListView when add or remove drive - AutoHotkey Community - Mozilla Firefox
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=80592
#Warn
#NoEnv
DetectHiddenWindows, On
Setworkingdir,%a_scriptdir%
setformat,float,0.2
SendMode Input
SetBatchLines -1
SetWinDelay, 0
filename1:="Active Drives"
Gui, -MaximizeBox -MinimizeBox +AlwaysOnTop
Gui,font,s10,Tahoma
Gui,Margin,0,0
Gui, Add, ListView, Grid R18 vLV1 w1180, Drive|STATUS|TYPE|FORMAT|Capacity-MB|FREE-MB|Capacity-GB|FREE-GB|Procent|SerialNR|LABEL|StatusCD
LV_ModifyCol(1 ,60)
LV_ModifyCol(2 , "100 Center")
LV_ModifyCol(3 , "100 Center")
LV_ModifyCol(4 , "100 Center")
LV_ModifyCol(5 , "100 Right")
LV_ModifyCol(6 , "100 Right")
LV_ModifyCol(7 , "100 Right")
LV_ModifyCol(8 , "100 Right")
LV_ModifyCol(9 , "100 Center")
LV_ModifyCol(10, "100 Right")
LV_ModifyCol(11, "100 Center")
LV_ModifyCol(12, "100 Center")
Gui,Show,w1180,%filename1%
gosub,drivegetshow
OnMessage(0x219, "notify_change")
Return
;---------------
GuiClose:
Gui,1:minimize
return
#IfWinExist Active Drives ahk_class AutoHotkeyGUI ahk_exe AutoHotkey.exe
ESC::ExitApp
#IfWinExist
;---------------
notify_change(wParam, lParam, msg, hwnd)
{
Gui,Show,w1180,Active Drives
gosub,drivegetshow
}
;---------------
drivegetshow:
LV_Delete()
DriveGet, A, List,
GuiControl,1: -Redraw,LV1
loop,parse,A
 {
      type1=
      cap1=
      free1=
      fs1=
      label1=
      serial1=
      status1=
      statusCD=
      drv=
      x:=a_loopfield
      drv=%x%:\
      DriveGet       , type1   , type     , %DRV%     ;- removable / fixed
      DriveGet       , cap1    , capacity , %DRV%
      DrivespaceFree , free1   ,            %DRV%
      DriveGet       , fs1     , fs       , %DRV%     ;- FAT
      DriveGet       , label1  , label    , %DRV%     ;- volume label >\\server1
      DriveGet       , serial1 , serial   , %DRV%     ;- volume serial number
      DriveGet       , status1 , status   , %DRV%
      DriveGet       , statusCD, StatusCD , %DRV%     ;- CDRom CDFS Audio CD open / stopped 
 prc   :=(free1*100)/cap1
 cap1a :=StrReplace(cap1 , Substr(cap1 , -2), "'" Substr(cap1 , -2))
 free1a:=StrReplace(free1, Substr(free1, -2), "'" Substr(free1, -2))
 cap2  :=Floor(cap1 /1024)
 free2 :=Floor(free1/1024)
 if (type1="CDROM")
   LV_Add("", drv,status1,type1,fs1,,,,,,serial1,label1,statuscd)
 else
   LV_Add("", drv,status1,type1,fs1,cap1a,free1a,cap2,free2,prc . "  %",serial1,label1,statuscd)
 }
GuiControl,1: +Redraw,LV1
return
;============================================
Bill
Posts: 32
Joined: 09 Sep 2017, 12:58

Re: Auto reload ListView when add or remove drive

06 Sep 2020, 17:04

Good idea. Thanks. At the moment I added a SetTimer which refreshes the display every few minutes. It's interesting to see how the Free MB count on the C: drive changes, mostly by just a few bytes, with no apparent drive access. Windows 10 is always actively doing something in the background.

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, Clemens375, lmstearn, mikeyww, Redbird and 41 guests