Link to home
Start Free TrialLog in
Avatar of Lionel MM
Lionel MMFlag for United States of America

asked on

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"
Avatar of Benjamin Voglar
Benjamin Voglar
Flag of Slovenia image

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
Avatar of Lionel MM

ASKER

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)
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Avatar of Bill Prew
Bill Prew

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
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"
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
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that works--returns
*DEBUG* Free space in bytes "29272608768"
*DEBUG* Free space in GB: "29"

How should I fairly award point to this now?
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
bill 350 and oBdA 150? ok w/both of you?
Sure, I'm fine with any split.

~bp
Fine with me.,
Thanks for the very good and very quick help.