Command Line - Help

Dalegolder1994

New member
Local time
8:12 AM
Messages
4
Hi Guy's and Girls,

Just first off I've never been too good with CMD and only an Apprentice in my company I work for.

Now something I've wanted to do for awhile was to make the following but I don't necassirealy I know how to do this in one batch file:

I would need a Batch file that is able to read the follow;
Find Current Hard Drive Space and What is used (Dir can do this as far as im aware)
Convert the CMD information Into either a Excel Document or a .txt format.


If anyone could lend me a hand with this that would be great.

P.S the drives I want to make a log of are Server Destinations so i can monitor their disk space daily.

Regards
New Member
Dale :)
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Prof 64bit
I don't know if this might help. I have a command line program that gives the free space in B KB MB GB using the largest appropriate. It's df.exe for disk free

[C:\Program Files\JPSoft\TCCLE13x64]df /?

df.exe 1.0.9.1 Copyright 2014 favessoft.com

Usage: df [ C D \\Name\Share ]
free disk space in one of B KB MB GB

( Truncated to 3 decimal places. )

example:
[C:\Program Files\JPSoft\TCCLE13x64]df c
383.848 GB Free on c:

If you would like to try it, it is free and may be downloaded from my page here:
Miles Ahead Software

Edit: you can use command line redirection to output to a file.
For example to append the result to test.txt you can do
df c: >>test.txt

Edit2: You may specify more than one drive on the line.
Example:
[C:\Program Files\JPSoft\TCCLE13x64]df c e
383.848 GB Free on c:
65.669 GB Free on e:

[C:\Program Files\JPSoft\TCCLE13x64]
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
I have just quickly put together the below code that outputs the free space in MB and GB if that's any use, it is currently set to output a txt file to the desktop

Code:
set tempfile=%userprofile%\desktop\Size.txt

for /f "tokens=1-3" %%n in ('"WMIC LOGICALDISK GET Name,Size,FreeSpace | find /i "C:""') do set free=%%n& set total=%%p
set free=%free:~0,-3%
set /a free=%free%/1049

echo ------------------------------------------- >%tempfile%
echo Local Disc Information >>%tempfile%
echo ------------------------------------------- >>%tempfile%
echo C: Space Free- %free% MB (approx)>>%tempfile%
set /a free=%free%/1024
echo C: Space Free- %free% GB (approx)>>%tempfile%

I will try and look into space used later.

Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
I'm going to have to admit... i have no idea what that big long for loop with what appears to be random characters means in the below code but it works :D

It does invoke poswershell if thats ok?
Again, i have set it to output to a file on the desktop.

Code:
SETLOCAL
set tempfile=%userprofile%\desktop\SpaceUsed.txt

FOR /F "usebackq tokens=1,2" %%f IN (`PowerShell -NoProfile -EncodedCommand "CgBnAHcAbQBpACAAVwBpAG4AMwAyAF8ATABvAGcAaQBjAGEAbABEAGkAcwBrACAALQBGAGkAbAB0AGUAcgAgACIAQwBhAHAAdABpAG8AbgA9ACcAQwA6ACcAIgB8ACUAewAkAGcAPQAxADAANwAzADcANAAxADgAMgA0ADsAWwBpAG4AdABdACQAZgA9ACgAJABfAC4ARgByAGUAZQBTAHAAYQBjAGUALwAkAGcAKQA7AFsAaQBuAHQAXQAkAHQAPQAoACQAXwAuAFMAaQB6AGUALwAkAGcAKQA7AFcAcgBpAHQAZQAtAEgAbwBzAHQAIAAoACQAdAAtACQAZgApACwAJABmAH0ACgA="`) DO ((SET U=%%f)&(SET F=%%g))

echo ------------------------------------------- >%tempfile%
echo Local Disc Information >>%tempfile%
echo ------------------------------------------- >>%tempfile%
echo C: Space Used- %U% GB (approx)>>%tempfile%
set /a free=%free%/1024
echo C: Space Free- %F% GB (approx)>>%tempfile%

Courtesy of a post here which also explains the above in more detail - Windows batch file to get C:\ drive total space and free space available - Stack Overflow

Example Output

-------------------------------------------
Local Disc Information
-------------------------------------------
C: Space Used- 75 GB (approx)
C: Space Free- 223 GB (approx)


Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
I've attempted one of the commands given and got it to export into a .txt document however it doesn't like \\ server commands for linking directly to a server (I'm not aware of the Drive Letters my Line Manager has set them as only as the Mapped letters of drives inside the main's.)

Example; \\****\Svr2
Map Letter: V:

Now I can do the maped letter but I need to be able to do it directly to the main drive.

If this needs explaining I can explain more just changing prices on our till system at the moment as well so it's two things at once :)
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Prof 64bit
I've attempted one of the commands given and got it to export into a .txt document however it doesn't like \\ server commands for linking directly to a server (I'm not aware of the Drive Letters my Line Manager has set them as only as the Mapped letters of drives inside the main's.)

Example; \\****\Svr2
Map Letter: V:

Now I can do the maped letter but I need to be able to do it directly to the main drive.

If this needs explaining I can explain more just changing prices on our till system at the moment as well so it's two things at once :)

These commands need running on the server, you appear to have the server mapped as a UNC path which will not work.

Regards,
Jamie
 

My Computer

Computer type
PC/Desktop
Computer Manufacturer/Model Number
Self Built
OS
Windows 8.1 Pro x64
CPU
Intel Core i5-2500K @ 3.30GHz - S1155
Motherboard
Asus P8P67 LE Rev3, Intel P67, S115
Memory
8GB Corsair DDR3 XMS3, PC3-12800
Graphics Card(s)
NVIDIA GeForce GTX 650
Sound Card
On-Board
Monitor(s) Displays
3 x 24" {Extended Display}
Screen Resolution
1920 x 1080
Hard Drives
300GB Seagate Barracuda 7200
PSU
550W Coolermaster GX550
Case
Silverstone Precision PS04B
Cooling
Stock
Keyboard
Logitech K120
Mouse
World of Warcraft Cataclysm MMO Gaming Mouse
Internet Speed
80 MB
Antivirus
MSE / Windows Defender
Browser
Chrome
That's what I Imagined, I've got the drives mapped however, I just need to be able to do it all Externally on a Client machine rather than a Server.
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Prof 64bit
Try this, but replace servername first

WMIC /node:"servername" LOGICALDISK GET Name,Size,FreeSpace
 

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
That actually worked thank you :)

I'll modify both commands given to me to export into a Word Doc and a Batch file to run by schedule.

Cheers guys!!
 

My Computer

Computer type
PC/Desktop
OS
Windows 7 Prof 64bit
I found an example and modified it so it calculates GB correctly + adds on option to list all drives for a server (or localhost).
The script takes 2 parameters.
1 = servername
2 = drive letter

If you only specify servername you'll see a list of drives.
If you also specify a drive letter it will display something like this:

Total space: 94GB
Free space: 58GB
Used space: 36GB
Percent Used: 38%
Percent Free: 62%

Code:
@ECHO OFF
IF "%~1"=="" goto help
IF "%~2"=="" goto list

@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION

@FOR /F "tokens=1-3" %%n IN ('"WMIC /node:"%1" LOGICALDISK GET Name,Size,FreeSpace | find /i "%2""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p

echo wsh.echo FormatNumber(cdbl(%TotalBytes%)/1024/1024/1024, 0) > %temp%.\tmp.vbs
for /f %%a in ('cscript //nologo %temp%.\tmp.vbs') do set TotalBytes=%%a
del %temp%.\tmp.vbs

echo wsh.echo FormatNumber(cdbl(%FreeBytes%)/1024/1024/1024, 0) > %temp%.\tmp.vbs
for /f %%a in ('cscript //nologo %temp%.\tmp.vbs') do set FreeBytes=%%a
del %temp%.\tmp.vbs

SET /A TotalSpace=!TotalBytes!
SET /A FreeSpace=!FreeBytes!
SET /A TotalUsed=%TotalSpace% - %FreeSpace%
SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
SET /A PercentFree=100-!PercentUsed!

IF %TotalSpace% LSS 0 goto error

@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=
goto end

:error
echo.
echo *** Invalid server or drive specified ***
echo.
goto help

:help
echo.
echo Queries remote server for free disk space.
echo Specify a MACHINENAME and a drive letter to be queried
echo.
echo Example:   MACHINENAME c:
echo.
goto end

:list
WMIC /node:"%1" LOGICALDISK GET Name,Size,FreeSpace
goto end

:end
 
Last edited:

My Computer

Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
Here's the source to df if you want to adapt it. The WinApiEx include file has all the API calls you need. It's included with AutoIt3 download package I believe.
On second thought I get compile errors. Constants already defined. So best to stick to vbscript? Hehe.
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
OK. I had a munged install of AutoIt3 that gave errors. I uninstalled it and installed the latest stable version and the Scite4AutoIt3 editor. These are free tools. Both may be downloaded here:https://www.autoitscript.com/site/autoit/downloads/

I compiled and ran this source successfully on both a Windows 7 x64 VM and on native Windows 8.0 x64. No errors or warnings.

Here's the source

Code:
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=HD.ico
#AutoIt3Wrapper_Outfile=df.exe
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_Description=Disk Free Space
#AutoIt3Wrapper_Res_Fileversion=1.0.9.3
#AutoIt3Wrapper_Res_LegalCopyright=2014  http://milesaheadsoftware.tk
#AutoIt3Wrapper_Res_Language=1033
#AutoIt3Wrapper_Res_Field=Productname|df
#AutoIt3Wrapper_Res_Field=Productversion|1.093
#AutoIt3Wrapper_Run_AU3Check=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <WinAPIEx.au3>
Opt('MustDeclareVars', 1)
Global $GB = 1024 * 1024 * 1024
Global $MB = 1024 * 1024
Global $KB = 1024
Global $div, $whole, $frac, $unit, $result, $drive, $len

If Not $CmdLine[0] Then
    $drive = StringLeft(@WorkingDir, 2)
    DisplaySpace()
    Exit
ElseIf $CmdLine[1] = "/?" Then
    ShowHelp()
Else
    For $x = 1 To $CmdLine[0]
        If StringLen($CmdLine[$x]) = 1 Then
            $drive = $CmdLine[$x] & ":"
        Else
            $drive = $CmdLine[$x]
        EndIf
        DisplaySpace()
    Next
EndIf

Func DisplaySpace()
    Local $Data = _WinAPI_GetDiskFreeSpaceEx($drive)
    If @error Then
        ConsoleWriteError("Error detecting free disk space on " & $drive & @CRLF)
        Return
    EndIf
    If $Data[2] >= $GB Then
        $div = $GB
        $unit = "GB"
    ElseIf $Data[2] >= $MB Then
        $div = $MB
        $unit = "MB"
    ElseIf $Data[2] >= $KB Then
        $div = $KB
        $unit = "KB"
    Else
        $div = 1
        $unit = "B "
    EndIf
    If $div = 1 Then
        $result = String($Data[2])
    Else
        $result = StringFormat("%.5f", $Data[2] / $div)
        If StringRight($result, 6) = ".00000" Then
            $result = StringLeft($result, StringInStr($result, ".") - 1)
        Else
            $result = StringLeft($result, StringInStr($result, ".") + 3)
        EndIf
    EndIf
    $result = StringFormat("%-9s", $result)
    $result &= $unit & " Free on "
    ConsoleWrite($result & $drive & @CRLF)
    If Not $CmdLine[0] Then
        ConsoleWrite(@TAB & "( df /? for help )" & @CRLF)
    EndIf
EndFunc   ;==>DisplaySpace

Func ShowHelp()
    Local $ver = FileGetVersion(@ScriptFullPath)
    If $ver = "0.0.0.0" Then
        $ver = ""
    EndIf
    ConsoleWrite(@CRLF & @ScriptName & " " & $ver & " Copyright " & @YEAR & " milesaheadsoftware.tk" & @CRLF)
    ConsoleWrite(@CRLF & "Usage: df [ C D \\Name\Share ]" & @CRLF & "free disk space in one of B KB MB GB" & @CRLF)
    ConsoleWrite(@CRLF & "( Truncated to 3 decimal places. )" & @CRLF & @CRLF)
    Exit
EndFunc   ;==>ShowHelp
 

My Computer

Computer Manufacturer/Model Number
HP Media Center
OS
Windows 7 32 bit
CPU
AMD 5200+ dual core
Memory
2 GB
Graphics Card(s)
NVidia GeForce 6150SE 128 MB
Monitor(s) Displays
CRT
Screen Resolution
1280x1024
Hard Drives
500 GB Sata internal :

SIIG USB 3.0 docking stations w/WD Caviar Black 6 Gb/s drives
Keyboard
PS/2
Mouse
PS/2 Wheel Mouse
Other Info
SIIG USB 3.0 PCIexpress card.
Back
Top