Change in script
Moderator: DosItHelp
Change in script
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?
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?
Re: Change in script
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:
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.
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.
Re: Change in script
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
@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
Re: Change in script
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?
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?
Re: Change in script
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).
Re: Change in script
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?
@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?
Re: Change in script
what is the value of the %FreeSpace% ?
Re: Change in script
Hi
I did it using VBS script. Will vbs script work for you?
I did it using VBS script. Will vbs script work for you?
Re: Change in script
You can post it also but I prefer batch script.
Re: Change in script
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