If disk space is less than 25GB then

Need help with a "simple" batch script please--want to run a command to rd (remove directory) when disk space on drive g is less than 25GB so need help with something like "if diskspace < 25GB then"
LVL 26
Lionel MMSmall Business IT ConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Benjamin VoglarIT ProCommented:
you can do it wit powershell.

get-volume | where {$_.SizeRemaining -lt 25}

Put it tu scheduler job.


$mes = get-volume | where {$_.SizeRemaining -lt 25}

send-mailmessage -smtp youtmail -from fff@ggg.ll -to sss@ggg.ll -Body $mes
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
I want it in a batch script and I don't want to be emailed; I want to then run a rd (as stated in my question)
0
Bill PrewCommented:
This should do what you want in a BAT script.  Adjust the SET lines at the top as needed.  A few notes:

- BAT files have limited precision for arithmetic,and WMIC reports free space in bytes, so it's not easy to convert that to pure GB (by dividing it by 1024^^3), so instead we just chop off the right 9 digits effectively dividing it by 1000^^3.  Typically for these type of scripts that is acceptable, and you can just adjust your purge threshold accordingly.

- I added a couple of displays for debugging, you can remove the *DEBUG* lines once you are done testing.

- I have the delete of the folder to purge commented out, and it will just display the RD command to the screen when you run it.  Again, this is for safety during testing.  Once you are comfortable it's working properly remove the word ECHO on that line.

- For testing run from a DOS command prompt window so you can see the output.

@echo off
setlocal

REM Define drive to check, freespace limit (in GB), and folder to remove when low on space
set Drive=G:
set Limit=25
set Remove=G:\TEMP

REM Try to get free space on desired drive
set Free=
for /f "skip=2 tokens=2 delims=," %%A in ('wmic LogicalDisk Where DeviceID^='%Drive%' Get FreeSpace /format:csv 2^>NUL') do set Free=%%A

REM If we couldn't get free space on drive do nothing
if "%Free%" EQU "" (
  echo *WARMING* Could not get free space for drive "%Drive%"
  exit /b
)

ECHO *DEBUG* Free space in bytes "%Free%"

REM Divide by 1000000000 (approximates converting to GB)
set Free=%Free:~0,-9%
if "%Free%" EQU "" set Free=0

ECHO *DEBUG* Free space in GB "%Free%"

REM If low on free space, remove desired folder
if %Free% LSS %Limit% (
  ECHO rd /s /q "%Remove%"
)

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Lionel MMSmall Business IT ConsultantAuthor Commented:
Hi Bill--think that will work but I think this needs to be
set Free=%Free:~0,-10% and not set Free=%Free:~0,-9%
if I use -9 I get 292 and not 29 to compare to 25, right?
0
Bill PrewCommented:
If you are getting 292, then that means you started with 292xxxxxxxxx bytes, or 292,xxx,xxx,xxx so that means there are 292GB free.  It needs to stay at -9 in the SET, not -10.

Let me know if still not clear.

~bp
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
I have 3 different disks--the first two are both 2TB and I am not too worried about them but then the 3rd is 650GB and right now is has 27GB free space--its is giving me the 292 free from your batch file. Windows Explore says 27GB free your batch file converts that into 292 that is why I though it needs to be 29 instead. Here are some of the output from running this batch

if "29272608768" EQU "" (
echo *WARMING* Could not get free space for drive "G:"
 exit /b
)

ECHO *DEBUG* Free space in bytes "29272608768"
*DEBUG* Free space in bytes "29272608768"
REM Divide by 1000000000 (approximates converting to GB)
set Free=292

if "292" EQU "" set Free=0

ECHO *DEBUG* Free space in GB "292"
*DEBUG* Free space in GB "292"
0
Bill PrewCommented:
Hmmm, can you post the script you are running back up please.  I did this test at my command line and it seems to validate the code I posted, and returns the 29.

Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\bprew>set Free=29272608768

C:\Users\bprew>echo %Free%
29272608768

C:\Users\bprew>set Free=%Free:~0,-9%

C:\Users\bprew>echo %Free%
29

Open in new window

~bp
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
all I did was "select all" on your script--copied and posted--used it exactly as is; did not make any adjustments (other than echo on), just ran it to see what number I would get and get 292 and not 29. I even recopied it directly and re-pasted it and still get 292
@echo off
setlocal

REM Define drive to check, freespace limit (in GB), and folder to remove when low on space
set Drive=G:
set Limit=25
set Remove=G:\TEMP

REM Try to get free space on desired drive
set Free=
for /f "skip=2 tokens=2 delims=," %%A in ('wmic LogicalDisk Where DeviceID^='%Drive%' Get FreeSpace /format:csv 2^>NUL') do set Free=%%A

REM If we couldn't get free space on drive do nothing
if "%Free%" EQU "" (
  echo *WARMING* Could not get free space for drive "%Drive%"
  exit /b
)

ECHO *DEBUG* Free space in bytes "%Free%"

REM Divide by 1000000000 (approximates converting to GB)
set Free=%Free:~0,-9%
if "%Free%" EQU "" set Free=0

ECHO *DEBUG* Free space in GB "%Free%"

REM If low on free space, remove desired folder
if %Free% LSS %Limit% (
  ECHO rd /s /q "%Remove%"
)

with echo off this is the result
*DEBUG* Free space in bytes "29272608768"
*DEBUG* Free space in GB "292"

and from the picture you can see that Drive G, according to Windows Explorer, is 27.2 GB free
EE-DiskSpace.jpg
0
oBdACommented:
If I may chime in: I could reproduce the problem with the initial script, where it told me that I had 535GB free on a 60GB drive.
The issue is not mathematics, as removing the last 9 characters is obviously correct when converting to billions, it's wmic.exe.
For whatever reason, wmic produces lines that end with <CR><CR><LF>. When parsing the output with "for /f" directly in Batch, the first <CR> ends up in the variable.
Two ways around this:
* redirect wmic's output to a file, and parse the file. When the output is written to file, the additional <CR>s are lost.
* Use nested "for /f" loops to break down the lines; the inner "for /f" will finally strip the <CR>.
I changed the original script, and removed the requirement to specify the drive separately, as logic would dictate that the folder to be removed resides on the drive that is to be monitored.
@echo off
setlocal

REM Define freespace limit (in GB), and folder to remove when low on space
set Remove=C:\TEMP
set Limit=25

REM Try to get free space on desired drive
for %%a in ("%Remove%") do set Drive=%%~da
set FreeSpaceBytes=
for /f "delims=" %%a in ('wmic.exe LogicalDisk Where DeviceID^='%Drive%' Get FreeSpace /value 2^>NUL ^| find.exe "="') do (
	for /f "tokens=1* delims==" %%d in ("%%a") do set FreeSpaceBytes=%%e
)

REM If we couldn't get free space on drive do nothing
if "%FreeSpaceBytes%" EQU "" (
	echo *WARNING* Could not get free space for drive "%Drive%"
	exit /b 1
)

ECHO *DEBUG* Free space in bytes "%FreeSpaceBytes%"

REM Divide by 1000000000 (approximates converting to GB)
set FreeSpaceGB=%FreeSpaceBytes:~0,-9%
if "%FreeSpaceGB%" EQU "" set FreeSpaceGB=0

ECHO *DEBUG* Free space in GB: "%FreeSpaceGB%"

REM If low on free space, remove desired folder
if %FreeSpaceGB% LSS %Limit% (
	ECHO rd /s /q "%Remove%"
)

Open in new window

1
Lionel MMSmall Business IT ConsultantAuthor Commented:
that works--returns
*DEBUG* Free space in bytes "29272608768"
*DEBUG* Free space in GB: "29"

How should I fairly award point to this now?
0
Bill PrewCommented:
Good catch oBdA, WMIC can be a challenge to parse...

@lionelmm, I'm fine with however you want to handle points.  I'd like to think I deserve some for the base script, but oBdA certainly contributed to a working version, so split them however you think is fair among contributors, I'll have no problems with whatever you decide, just happy you got something that meets your need.

~bp
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
bill 350 and oBdA 150? ok w/both of you?
0
Bill PrewCommented:
Sure, I'm fine with any split.

~bp
0
oBdACommented:
Fine with me.,
0
Lionel MMSmall Business IT ConsultantAuthor Commented:
Thanks for the very good and very quick help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.