Efficient Code

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Efficient Code

#1 Post by Jbcom41 » 26 Sep 2021 11:28

Hi Folks

I have created the following batch file below that runs when users login to their systems, can someone please look over my code and let me know if this is the most efficient way? or give advice to make it more efficient?

John

Code: Select all

for /f "delims=" %%i in ('WMIC CSPRODUCT GET NAME /value') do for /f "delims=" %%j in ("%%i") do set "%%j"
for /f "delims=" %%i in ('wmic bios get serialnumber /value') do for /f "delims=" %%j in ("%%i") do set "%%j"
set ip_address_string="IPv4 Address"
for /f "usebackq tokens=2 delims=:" %%f in (`ipconfig ^| findstr /c:%ip_address_string%`) do (
@(Echo Event,Date,Time,Device,User,IP,SN,Model & Echo Login,%DATE%,%TIME:~0,8%,%COMPUTERNAME%,%USERNAME%,%%f,%SerialNumber:~0,20%,%NAME%))1> //PC500/Demo/Source/%random%.csv
goto :eof

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Efficient Code

#2 Post by Compo » 26 Sep 2021 17:37

If there is always only one IP Address, and at login that address is already determined, and both of your shown WMIC queries return actual values, i.e. they are never empty, then I would say it could certainly be made more efficient.

The reason is that WMIC.exe is historically a relatively slow command to run, so you could speeed things up by only calling it once. Given that idea, you may as well retrieve your IP Address using it too, instead of running and parsing another command utility, IPCONFIG.

So this idea would run WMIC only once, and parse it in just a single FOR loop, instead of the five FOR loops you've implemented:

Code: Select all

@SetLocal EnableExtensions
@For /F Tokens^=6-10^ Delims^=^" %%G In ('
 (	Echo(NICConfig Where IPEnabled^='TRUE' Get IPAddress /Format:MOF ^&
	Echo(BIOS Get SerialNumber /Format:MOF ^&
	Echo(CSProduct Get Name /Format:MOF^) ^| %SystemRoot%\System32\wbem\WMIC.exe
') Do @If "%%K" == "" (If Defined SN (Set "PC=%%G") Else Set "SN=%%G"
) Else Set "IP=%%I"
@(	Echo Event,Date,Time,Device,User,IP,SN,Model
	Echo Login,%DATE%,%TIME:~,8%,%COMPUTERNAME%,%USERNAME%,%IP%,%SN:~,20%,%PC%
) 1>"//PC500/Demo/Source/%RANDOM%.csv"
I've also used the full path to the location of the WMIC utility, as leaving it out would mean that the file would need to be searched within the current directory, and if not located there within each location listed within %PATH%, in order first to last, until found. I have also included the extension for the utility too, as missing it out means that each extension listed within %PATHEXT%, in order first to last, would need to be suffixed during such a search, until a match is found. Obviously both of these tasks must consume unnecessary time and resources, which I'd deem inefficient.

Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Re: Efficient Code

#3 Post by Jbcom41 » 27 Sep 2021 04:14

thanks Compo for that much appreciated.

Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Re: Efficient Code

#4 Post by Jbcom41 » 27 Sep 2021 09:34

Hi Compo

Is there anyway to remove the last , comma from the data given or back slash so its just the data e.g. below?

many Thanks

John

Event,Date,Time,Device,User,IP,SN,Model
Login,27/09/2021,15:05:55,LT6795,ssmiht,192.168.0.221,VMware-67 0t 90 50 9,VMware/7,1

Event,Date,Time,Device,User,IP,SN,Model
Login,27/09/2021,15:05:55,LT6795,ssmiht,192.168.0.221,VMware-67 0t 90 50 9,VMware71

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

Re: Efficient Code

#5 Post by Compo » 27 Sep 2021 17:28

Just perform a variable expansion and substitution on %PC%.

The following snippet would replace both / and , with nothing

Code: Select all

…
@Set "PC=%PC:/=%"
@(	Echo Event,Date,Time,Device,User,IP,SN,Model
	Echo Login,%DATE%,%TIME:~,8%,%COMPUTERNAME%,%USERNAME%,%IP%,%SN:~,20%,%PC:,=%
) 1>"//PC500/Demo/Source/%RANDOM%.csv"
Or if you want to replace the / with a _ and the , with a .

Code: Select all

…
@Set "PC=%PC:/=_%"
@(	Echo Event,Date,Time,Device,User,IP,SN,Model
	Echo Login,%DATE%,%TIME:~,8%,%COMPUTERNAME%,%USERNAME%,%IP%,%SN:~,20%,%PC:,=.%
) 1>"//PC500/Demo/Source/%RANDOM%.csv"

Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Re: Efficient Code

#6 Post by Jbcom41 » 28 Sep 2021 08:46

Thanks Compo that worked a treat...

Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Re: Efficient Code

#7 Post by Jbcom41 » 29 Sep 2021 04:45

Hi Compo

the script works a treat but there is one PC on the netowrk, its a HP Compaq Pro 4300 SFF PC that seems to be gving starange data output please see below:

Event,Date,Time,Device,User,IP,SN,Model
Logout,28/09/2021,23:20:12,KM9875,JKane,,~,19PC:,=

The rest of the PCs are fine, could you please check my script below. maybe I have done sometihng silly?

Thanks

John

@SetLocal EnableExtensions
@For /F Tokens^=6-10^ Delims^=^" %%G In ('
( Echo(NICConfig Where IPEnabled^='TRUE' Get IPAddress /Format:MOF ^&
Echo(BIOS Get SerialNumber /Format:MOF ^&
Echo(CSProduct Get Name /Format:MOF^) ^| %SystemRoot%\System32\wbem\WMIC.exe
') Do @If "%%K" == "" (If Defined SN (Set "PC=%%G") Else Set "SN=%%G"
) Else Set "IP=%%I"
@Set "PC=%PC:/=%"
@( Echo Event,Date,Time,Device,User,IP,SN,Model
Echo Login,%DATE%,%TIME:~,8%,%COMPUTERNAME%,%USERNAME%,%IP%,%SN:~,19%,%PC:,=%
) 1>"//PC500/Demo/Source/%RANDOM%.csv"

Jbcom41
Posts: 11
Joined: 20 Jun 2021 02:36

Re: Efficient Code

#8 Post by Jbcom41 » 01 Oct 2021 03:54

Hi Compo

got walk around, thanks for all the help, much appreciated...

Kind Regards

John

Post Reply