Change in script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Change in script

#1 Post by mor.bas » 29 Jan 2013 02:17

Hi,
I have this script:

@ECHO OFF
@COLOR 7
@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION
@CLS
@FOR /F "tokens=1-3" %%n IN ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "%1""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p
@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!
@ECHO Total space: %TotalSpace%GB
@ECHO Free space: %FreeSpace%GB
@ECHO Used space: %TotalUsed%GB
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%
@SET TotalSpace=
@SET FreeSpace=
@SET TotalUsed=
@SET PercentUsed=
@SET PercentFree=
IF %FreeSpace% LSS 20 echo bad

I want to check if free space is less then 20 gb and than echo bad

I got 20 was unexpected this time

How to fix it?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Change in script

#2 Post by abc0502 » 29 Jan 2013 02:31

What %1 should be in the for command ??

You set the FreeSpace to nothing, so when you compare to see if it less than 20 it will always be:
@SET TotalSpace=
@SET FreeSpace=
@SET TotalUsed=
@SET PercentUsed=
@SET PercentFree=
IF %FreeSpace% LSS 20 echo bad

Also, check if FreeSpace doesn't contain extra space at the end by echo %FreeSpace%X see if there is space before the X character or not.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: Change in script

#3 Post by shirulkar » 29 Jan 2013 02:47

I have edited your script as below. One question why you were assigning nothing to following variables:

@SET TotalSpace=
@SET FreeSpace=
@SET TotalUsed=
@SET PercentUsed=
@SET PercentFree=

and you used "%FreeSpace%" after assigning nothing to %FreeSpace% in IF sentence?

@ECHO OFF
@COLOR 7
@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION
@CLS
@FOR /F "tokens=1-3" %%n IN ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "%1""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p
@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!
@ECHO Total space: %TotalSpace%
@ECHO Free space: %FreeSpace%
@ECHO Used space: %TotalUsed%
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%
IF %FreeSpace% LSS 20 echo bad

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Change in script

#4 Post by mor.bas » 29 Jan 2013 03:05

Hi,
The nothing about the variable it for something else...
My question is now the giga byte space there is round but I want to check if it's exactly less then 4 GB (4096 MB)
How to fix it?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Change in script

#5 Post by abc0502 » 29 Jan 2013 03:09

you can't check this value if you set it to nothing as it will always be, however you can set it after the if command but not before it. (This is the Problem, put the if command before starting to set the variables to nothing).

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Change in script

#6 Post by mor.bas » 29 Jan 2013 03:17

hat I understand I changed it to this :

@ECHO OFF
@COLOR 7
@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION
@CLS
@FOR /F "tokens=1-3" %%n IN ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "%1""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p
@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!
@ECHO Total space: %TotalSpace%GB
@ECHO Free space: %FreeSpace%GB
@ECHO Used space: %TotalUsed%GB
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%
IF %FreeSpace% LSS 4 echo bad

But my question was that now it's round up but i want to check exactly that it's less then 4 GB (4096)
What to change?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Change in script

#7 Post by abc0502 » 29 Jan 2013 03:19

what is the value of the %FreeSpace% ?

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: Change in script

#8 Post by shirulkar » 29 Jan 2013 03:23

Hi
I did it using VBS script. Will vbs script work for you?

mor.bas
Posts: 66
Joined: 25 Apr 2012 04:28

Re: Change in script

#9 Post by mor.bas » 29 Jan 2013 05:26

You can post it also but I prefer batch script.

shirulkar
Posts: 40
Joined: 15 Jan 2013 23:53

Re: Change in script

#10 Post by shirulkar » 29 Jan 2013 05:33

Below is the VBS script

Code: Select all

'declaring the constants and setup the winmgmts service'
Const GB = 1073741824
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
Set colDisks = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")

'Checking the disk space for each Drive'
For Each objDisk in colDisks
numDiskSpace = objDisk.Size
numFreeSpace = objDisk.FreeSpace
WScript.Echo numFreeSpace
numfreepercent=Left((numFreeSpace / numDiskSpace) * 100,4)
WScript.Echo vbCrLf & "Disk Name: " &objDisk.Name
WScript.Echo " Disk Size: " & Left(numDiskSpace/GB,4) & " GB"
WScript.Echo " Free Space: " & Left(numFreeSpace/GB,4) & " GB"
WScript.Echo " percent free: " & LEFT(numfreepercent/1,2) & "%"

'Checking the disk usage,if more than 50% of the total available space then send a notification through e mail.'
If numfreepercent < 4.0 Then
 Wscript.ECHO "Free Space not avaible"
ELSE
WScript.Echo " there is sufficient space to use in the disk."
End If
Next

Post Reply