Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need to run a program which needs to find how much free space I have in a specific drive ( unc or regular)

I need it to work under CMD.( no ps)

Goal :

c:\> something e:
     1902323213 //nevermind mb /gb

please notice : numbers only ! I dont have the tool to deal with strings ( in my soft)

So what have I tried ?

I've tried this :

DIR \\dsmain\ls_BackUp\*.* | find "free"

which yields :

2 Dir(s) 311,285,014,528 bytes free

I've also tried this : (worst)

fsutil volume diskfree c:

which yields :

Total # of free bytes        : 54761066496
Total # of bytes             : 255953203200
Total # of avail free bytes  : 54761066496

Question :

Using cmd (no ps) - how can I run a command which returns only the free space of a drive ?

nb :

Sql server needs to open a bak file on remote location and i need to check free space there , so sql execute cmd command and i can capture the result . So i dont want to start using string manipulations to extract the number

share|improve this question
    
What are you trying to do that getting the free space from a command window is the solution? (There may be a easier way to do it) –  Scott Chamberlain Mar 19 at 17:49

3 Answers 3

You can use the command line tool wmic to query Win32_LogicalDisk

wmic LOGICALDISK where name="C:" get freespace

That will return two lines in the format

FreeSpace
2881409024

or

wmic LOGICALDISK where name="C:" get freespace | find /v "Free"

which yields just the number

share|improve this answer
    
Sql server needs to open a bak file on remote location and i need to check free space there , so sql execute cmd command and i can capture the result . So i dont want to start using string manipulations to extract the number .... The first solution however its multiple statements .... Yours is once but you have 2 lines ... Cant you find away to return please only the second row ? –  Royi Namir Mar 19 at 18:17
1  
@RoyiNamir if that is all you want to do you can use the built in stored procedure xp_fixeddrives just do exec master..xp_fixeddrives. Things like this is why it is important to ask your real problem, not asking about your problem you have with the solution you came up with to your question (The XY Problem) –  Scott Chamberlain Mar 19 at 18:45
    
- not on remote locations ( I did used it locally when I restore the bak - but when I need to SAVE the bak on remote location I need to validate if it has a free space.) this command(which i know) is not helping for remote locations. ( and dont tell me to use map network drive:-)) –  Royi Namir Mar 19 at 18:47
    
In that case look at this blog post the poster uses a similar wmic command to what I posted in my answer and turns it in to a stored procedure, you can tweak his code if you need to do execute whatever wmic command that you need and get a result in a table structure that will let you filter and choose whatever columns from the result you need. –  Scott Chamberlain Mar 19 at 18:48
    
@tnx , i will try extract the second line from your statement (and ofcourse will look at your link) –  Royi Namir Mar 19 at 18:50

The below returns JUST the free space in bytes.

@setlocal enableextensions enabledelayedexpansion
@echo off
for /f "tokens=3" %%a in ('dir c:\') do (
    set bytesfree=%%a
)
set bytesfree=%bytesfree:,=%
echo %bytesfree%
endlocal && set bytesfree=%bytesfree%
pause>nul
share|improve this answer
    
this is not working. (win8 cmd) –  Royi Namir Mar 19 at 18:34
    
Manually replacing a hard-coded thousand separator is not a good idea, especially considering it can be different in non-English locales, and it can be further customized by users. Better use the /-c parameter to remove those separators entirely. Also, single quotes aren't enough when the target path contains spaces. –  and31415 Mar 19 at 21:27

Here's a one-line command you can use in a command prompt:

for /f "tokens=3" %G in ('"dir /-c "\\dsmain\ls_BackUp\" "') do @set free=%G

To display the free space value you can echo it:

echo %free%

How it works

The output of the dir command is parsed line by line till the end. The last line is always the one which contains the free space amount, unless the path is invalid. To avoid having to deal with thousands separators (which are locale-aware, and can be customized), you need to use the /-c parameter to strip them entirely. Each line gets tokenized by using spaces as separators, and the third token is then assigned to the %free% variable each time.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.