retrieve system information

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

retrieve system information

24 May 2018, 10:21

Is it possible to retrieve trough ahk some data about the system and instert it in a text file?
something like :
Processor:
Installed memory:
OS version and type:
Free space on system drive:
I'm sure some of this data can be retrieved via wmic commands, but wanted to go around that if ahk can do it without using wmic.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: retrieve system information

24 May 2018, 12:29

MsgBox, % A_OSVersion ; thats built in

RAM info see Skan https://autohotkey.com/board/topic/3362 ... installed/

Free space https://autohotkey.com/docs/commands/DriveSpaceFree.htm

You have to search for each if you want to avoid WMI
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

24 May 2018, 14:12

cool. thanks.
how about avoiding storing it in a text file?
I was thinking about less footprints as possible and integrating the values in an email message or creating the text on a remote location (sftp or ftp) without creatinging it first on the host system.

LE: any way to make drivespace free work with system variables ? I see it only acceptas C:\ D:\ etc, but not %systemdrive% variable... also how would one go about retrieving the left space in GB?
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

26 May 2018, 23:37

Seems that wmic is still a better way to retrieve the data I need.
how can I retrieve the output of a "wmic" command and store it as a variable?
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: retrieve system information

27 May 2018, 00:04

apoklyps3 wrote:Seems that wmic is still a better way to retrieve the data I need.
how can I retrieve the output of a "wmic" command and store it as a variable?
this might help https://autohotkey.com/board/topic/6096 ... ith-ahk-l/

You could also download WMI code creator to find more info and build your own scripts for what you need
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: retrieve system information

27 May 2018, 00:12

You don't need the command line tool.

Code: Select all

For Processor in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Processor") {
    MsgBox % Processor.Name
}
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

27 May 2018, 05:53

awesome examples.
@Alguimist could you help me adapt the code you provided to get:
Memory Size in GB
Free space in GB on systemdrive
?

I would have used DriveFreeSpace, but it doesn't seem to work with env variables only fixed paths.
Thanks in advance
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: retrieve system information

27 May 2018, 08:41

using Alguimist's approach, read WMI Tasks: Computer Hardware
to figure out what queries to run and what properties to access
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

27 May 2018, 14:08

I would try to do that , but it's still unclear to me how I transform this to a variable.
I have tried the said example with FileAppend and I get that a variable has an illegal character if I enclose it in % (guessing the dot is the illeagal character)
and If I write it as % Processor.Name I get that it's is missing an ending %.
Super unclear to me how to use this other than the msgbox example that works flawlessly
garry
Posts: 3743
Joined: 22 Dec 2013, 12:50

Re: retrieve system information

27 May 2018, 14:28

DriveGet , show drives

Code: Select all

DriveGet, A, List,
Loop,Parse,a
{
      type1=
      cap1=
      free1=
      fs1=
      label1=
      serial1=
      status1=
      drv=
      x:=a_loopfield
      drv=%x%:\
      DriveGet, type1, type, %DRV%
      DriveGet, cap1, capacity, %DRV%
      DrivespaceFree, free1, %DRV%
      DriveGet, fs1, fs, %DRV%             ;FAT
      DriveGet, label1, label, %DRV%       ;volume label >\\server1   or CAM_HDD
      DriveGet, serial1, serial, %DRV%     ;volume serial number
      DriveGet, status1, status, %DRV%
      if (Label1="")
          Label1=NoName
      e .= drv . "----------------------`n" "_Type=" . type1 . "`n_SerialNr=" . serial1 . "`n_Name=" . label1 . "`n_Format=" . fs1 . "`n_Capacity=" . cap1 . "-MB" . "`n_Free=" . free1 . "-MB" . "`n_Status=" . status1 . "`r`n`r`n"
}
msgbox,%e%
e=
exitapp
;===============================================================

script from user jNizM , MEMORY = total / free / used / clear

Code: Select all

; ===================================================================================
; AHK Version ...: AHK_L 1.1.13.00 x64 Unicode
; Win Version ...: Windows 7 Professional x64 SP1
; Description ...: Memory
; : Total, Free & Used Memory, Clear Memory
; Version .......: 2013.10.04-1456
; Author ........: jNizM
; License .......: WTFPL
; License URL ...: http://www.wtfpl.net/txt/copying/
; ===================================================================================
;@Ahk2Exe-SetName Memory
;@Ahk2Exe-SetDescription Memory
;@Ahk2Exe-SetVersion 2013.10.04-1456
;@Ahk2Exe-SetCopyright Copyright (c) 2013`, jNizM
;@Ahk2Exe-SetOrigFilename Memory.ahk
 
; GLOBAL SETTINGS ===================================================================
 
#Warn
#NoEnv
#SingleInstance Force
 
; SCRIPT ============================================================================
 
love := chr(9829)
 
Gui, Margin, 10, 10
Gui, Font, s9, Courier New
Gui, Add, Text, xm ym w110 h23 0x200, Total Memory:
Gui, Add, Text, xm+115 ym w100 h23 0x202 vTMemory,
Gui, Add, Text, xm ym+25 w110 h23 0x200, Free Memory:
Gui, Add, Text, xm+115 ym+25 w100 h23 0x202 vFMemory,
Gui, Add, Progress, xm+220 ym+26 h20 r0-10 0x01 BackgroundC9C9C9 c5BB75E vPFMemory,
Gui, Add, Text, xm ym+50 w110 h23 0x200, Used Memory:
Gui, Add, Text, xm+115 ym+50 w100 h23 0x202 vUMemory,
Gui, Add, Progress, xm+220 ym+51 h20 r0-10 0x01 BackgroundC9C9C9 cDA4F49 vPUMemory,
Gui, Add, Text, xm ym+80 w358 0x10
Gui, Add, Text, xm ym+90 w110 h23 0x200, Cleared Memory:
Gui, Add, Text, xm+115 ym+90 w100 h23 0x202 vCMemory,
Gui, Add, Button, xm+255 ym+90 w100 h23 -Theme 0x8000 gClearMem, Clear Memory
Gui, Font, cSilver,
Gui, Add, Text, xm ym+120 w250 h21 0x200, made with %love% and AHK 2013, jNizM
Gui, Show, AutoSize, MemoryInfo v0.1
 
SetTimer, GetMemory, 1000
return
 
GetMemory:
TVMemory := Round(WMI_MEMORY(1), 2)
FPMemory := Round(WMI_MEMORY(2), 2)
UPMemory := Round(WMI_MEMORY(3), 2)
 
GuiControl,, TMemory, % TVMemory . " MB"
GuiControl,, FMemory, % FPMemory . " MB"
GuiControl,, UMemory, % UPMemory . " MB"
 
GuiControl +Range0-%TVMemory%, PFMemory
GuiControl,, PFMemory, % FPMemory
Free := % Round(FPMemory / TVMemory * 100)
if Free between 0 and 19
GuiControl, +cDA4F49, PFMemory
else if Free between 20 and 39
GuiControl, +cDA9849, PFMemory
else if Free between 40 and 69
GuiControl, +cE3E789, PFMemory
else if Free between 70 and 100
GuiControl, +c5BB75E, PFMemory
 
GuiControl +Range0-%TVMemory%, PUMemory
GuiControl,, PUMemory, % UPMemory
Used := % Round(UPMemory / TVMemory * 100)
if Used between 0 and 30
GuiControl, +c5BB75E, PUMemory
else if Used between 31 and 50
GuiControl, +cE3E789, PUMemory
else if Used between 51 and 80
GuiControl, +cDA9849, PUMemory
else if Used between 81 and 100
GuiControl, +cDA4F49, PUMemory
return
 
ClearMem:
FPMemoryA := Round(WMI_MEMORY(2), 2)
ClearMemory()
FreeMemory()
FPMemoryB := Round(WMI_MEMORY(2), 2)
GuiControl,, CMemory, % Round(FPMemoryB - FPMemoryA, 2) . " MB"
return
 
; FUNCTIONS =========================================================================
 
WMI_Memory(mem) {
objWMIService := ComObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
colItems := objWMIService.ExecQuery("Select * from Win32_OperatingSystem")._NewEnum
while colItems[objItem]
{
return, % (mem = "1") ? objItem.TotalVisibleMemorySize / 1024
: (mem = "2") ? objItem.FreePhysicalMemory / 1024
: (mem = "3") ? (objItem.TotalVisibleMemorySize - objItem.FreePhysicalMemory) / 1024
: "FAIL"
}
}
 
ClearMemory() {
for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") {
handle := DllCall("OpenProcess", "UInt", 0x001F0FFF, "Int", 0, "Int", process.ProcessID)
DllCall("SetProcessWorkingSetSize", "UInt", handle, "Int", -1, "Int", -1)
DllCall("CloseHandle", "Int", handle)
}
return
}
 
FreeMemory() {
return, DllCall("psapi.dll\EmptyWorkingSet", "UInt", -1)
}
 
; EXIT ==============================================================================
 
GuiClose:
GuiEscape:
ExitApp
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: retrieve system information

27 May 2018, 14:30

Code: Select all

For Processor in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Processor") {
    FileAppend, % Processor.Name, output.txt
}
User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: retrieve system information

27 May 2018, 17:17

apoklyps3 wrote:I would have used DriveFreeSpace, but it doesn't seem to work with env variables only fixed paths.
If you have #NoEnv in your script, %SystemDrive% will be blank. You can replace it with A_WinDir or expand the environment variable with DllCall:

Code: Select all

VarSetCapacity(Dest, 512)
Src := "%SystemDrive%"
DllCall("ExpandEnvironmentStrings", "Str", Src, "Str", Dest, "Int", 512)
DriveSpaceFree Free, %Dest%
MsgBox % Free
jNizM wrote another script where memory information is not retrieved with WMI.
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

28 May 2018, 05:14

Many thanks for all the examples . Thanks to all of you my script is almost complete. Only two problems left:
1. I have no Ideea how to line break in an expression(`n doesn't seem to be valid). My current script appends everything together)

Code: Select all

For Processor in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Processor") {
    FileAppend, % Processor.Name, PC.txt
}
For ComputerSystem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_ComputerSystem") {
    FileAppend,	% ComputerSystem.TotalPhysicalMemory, PC.txt
}
2. I have no ideea how to change the code so that TotalPhysicalMemory is reflected in GB
same goes for DriveSpaceFree
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: retrieve system information

28 May 2018, 05:36

apoklyps3 wrote:Many thanks for all the examples . Thanks to all of you my script is almost complete. Only two problems left:
1. I have no Ideea how to line break in an expression(`n doesn't seem to be valid). My current script appends everything together)

Code: Select all

For Processor in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Processor") {
    FileAppend, % Processor.Name, PC.txt
}
For ComputerSystem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_ComputerSystem") {
    FileAppend,	% ComputerSystem.TotalPhysicalMemory, PC.txt
}
2. I have no ideea how to change the code so that TotalPhysicalMemory is reflected in GB
same goes for DriveSpaceFree
I can't answer the GB question but here is a sample for line breaks and adding other text if you need

Code: Select all

For Processor in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_Processor") {
    FileAppend, % Processor.Name " By The sheep" "`n", PC.txt
}
For ComputerSystem in ComObjGet("winmgmts:").ExecQuery("SELECT * FROM Win32_ComputerSystem") {
    FileAppend,	% "For The goats " ComputerSystem.TotalPhysicalMemory "`n", PC.txt
}
apoklyps3
Posts: 116
Joined: 13 Feb 2016, 13:20

Re: retrieve system information

28 May 2018, 05:55

Many thanks.
Managed to transform to GB too using Round and / 1024**3
:)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: retrieve system information

29 May 2018, 02:10

- Re. enters, try "`r`n" instead of "`n" (CRLF instead of LF).
- Sometimes if you open a file in Notepad, it looks like it has no line breaks, this is because it's LF-delimited, not CRLF-delimited. You could try opening the file in WordPad instead.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Ardalion, Google [Bot] and 164 guests