Batch file - reporting HDD space

Microsoft Microsoft windows xp pro full...
March 10, 2010 at 22:54:52
Specs: Windows XP
a batch file to report HDD space of different windows servers. send Report (.csv or .xls) to a mail id and should consists of below:
1. IP Number of the server
2. Name of the server
3. Able to Ping (y/n)
4. Total capacity of each drive
5. Total Free space of each drive
6. Time stamp

See More: Batch file - reporting HDD space

Report •


#1
March 10, 2010 at 23:18:03
1. Ipconfig
2. %computername%
3. ping || / if errorlevel
4. Fsutil(if ntfs) (bytes)
5. see 4
6. %date% %time%

i. For /f loop with 1,4,5 and possibly 6.
ii. Network permissions.
iii. Batch isn't really the best for this.


Report •

#2
March 10, 2010 at 23:33:57
me very new to batch, learning how to write batch, can you give me full code, Thanks in advance.

Report •

#3
March 11, 2010 at 00:00:19
I *could* do most of it(no network to test on), but then you would still be "very new". Someone else may do it but I won't out and write it without you even attempting it.

Report •

Related Solutions

#4
March 11, 2010 at 01:17:06
Hi Thank you for your reponse.

Below is the code.
::space_ping: Phani

@echo off

for /f "tokens=*" %%I in (IPlist) do
( Call :ipconf %%I )
( call :pinger %%I )
( Call : FSutil %%I)
setLocal EnableDelayedExpansion
set /a totalspace=!totalused!+!freespace!
set /a percentused=(!totalused!*100)/!totalspace!
set /a percentfree=100-!percentused!
echo Total space: %totalspace% GB>>space.txt
echo Free space: %freespace% GB (%percentfree%%%)>>space.txt
echo Used space: %totalused% GB (%percentused%%%)>>space.txt
goto :eof

:pinger
echo %TIME% >> pingLOG
ping %1 | find /i "reply" > nul
if errorlevel 1 echo %1 >> pingLOG

:ipconf
for /f "tokens=*" %%I in (IPlist) do
echo %%I
echo ipconfig/all >>ipconf.txt

:fsutil
for /f "tokens=*" %%I in (IPlist) do
echo %%I
echo fsutil >>util.txt

:end
Iplist.txt contains the IP address, initially i had entered only 3 ip address that am able to ping from my system.


Report •

#5
March 11, 2010 at 01:22:23
changed a bit
::space_ping: Phani

@echo off

for /f "tokens=*" %%I in (IPlist) do
( Call :ipconf %%I )
( call :pinger %%I )
( Call : FSutil %%I)
setLocal EnableDelayedExpansion

goto :eof

:pinger
echo %TIME% >> pingLOG
ping %1 | find /i "reply" > nul
if errorlevel 1 echo %1 >> pingLOG

:ipconf
for /f "tokens=*" %%I in (IPlist) do
echo %%I
echo ipconfig/all >>ipconf.txt

:fsutil
set /a totalspace=!totalused!+!freespace!
set /a percentused=(!totalused!*100)/!totalspace!
set /a percentfree=100-!percentused!

for /f "tokens=*" %%I in (IPlist) do
echo %%I
echo fsutil >>util.txt
echo Total space: %totalspace% GB>>space.txt
echo Free space: %freespace% GB (%percentfree%%%)>>space.txt
echo Used space: %totalused% GB (%percentused%%%)>>space.txt
:end


Report •

#6
March 11, 2010 at 03:41:50
Ok, I have been looking into this and it seems a little more complicated than at first look. The real problem is that you have ip addresses when your probably going to need computernames. It may be possible to get the computernames from ping or nslookup, buit I'm not sure of this and can't test it.

Also since you want to know information about the servers you are going to need to run commands on them remotely, for this I suggest psexec.exe, if this is out of the question then I have no ideas.

Can you please tell me if either of these commands outputs the network computer names:

ping <ip address of computer>
nslookup <ip address of computer>

If these can't get the computername then I have no idea of how this can work.

It's all relatively easy to do from the machine itself but trying to do it remotely isn't really my thing, I will still try to help if I can.


Report •

#7
March 11, 2010 at 08:19:28
HMM. does this code work? I noticed some bugs...

::space_ping: Phani
@echo off 

rem You need to initialize your vars at the top to prevent undefined vars.
setLocal EnableDelayedExpansion
set /a totalspace=0
set /a percentused=0
set /a percentfree=0
set /a totalused=0
set /a freespace=0

rem You should initialize the files.
echo Tested Following IPs: > pingLOG.txt
echo Current IP Config: > ipconf.txt
echo Disk Info: > util.txt
echo Disk Space Ussage: > space.txt

Call :pinger 
Call :ipconf 
Call :FSutil 
Call :calcpercentages
goto :eof

:calcpercentages
  rem you need code to pars the util.txt
  set /a totalspace=!totalused!+!freespace!
  set /a percentused=(!totalused!*100)/!totalspace!
  set /a percentfree=100-!percentused!
  echo Total space: %totalspace% GB>>space.txt
  echo Free space: %freespace% GB (%percentfree%)%%>>space.txt 
  echo Used space: %totalused% GB (%percentused%)%%>>space.txt
goto :eof

:pinger
  for /f "tokens=*" %%I in (IPlist.txt) do ( 
    echo Tested %1 on %TIME% >> pingLOG.txt
    echo Errors Found: >> pingLOG.txt
    ping %%I | find /i "reply" > nul
    if errorlevel 1 echo %%I >> pingLOG.txt
  )
goto :eof

:ipconf
  ipconfig/all >>ipconf.txt
goto :eof

:fsutil
  fsutil volume diskfree c:>>util.txt
goto :eof

I started commenting the bugs bit there where so many that I just rewrote your code. You still need code to pars the disk space results. I will work on it later.


Report •

#8
March 11, 2010 at 09:24:40
Thank you for rewriting the code,ace_omega
I too tested the code and received error messages, shall be posting them.
Judago
Exactly,you are correct, i do have the ip addresses from which i would like to receive the computer name. I shall be testing for nslookup and ping. I will be checking with psexec and post the results.

Report •

#9
March 11, 2010 at 09:26:30
O.K. now I am stuck. I made the following to pars the results of the disk info but the Command Prompt is based off of DOS and old system written for old computers and therefore does not support FLOATS or WORDS for numeric veriables. This is a problem because it can not seem to do math on numbers larger than 32000 or maybe I am missing something.

My Code...

:fsutil
  fsutil volume diskfree c:>util.txt
  for /f "tokens=1-3 delims=:" %%A in (util.txt) do (
    call :SetVar %%B %%A
  )
goto :eof

:SetVar
  echo %1 %5
  if "%5" == "free" set /a totalused=%1
  if "%5" == "bytes" set /a totalspace=%1
  if "%5" == "avail" set /a freespace=%1
  pause
goto :eof

Maybe some one more advanced can help.


Report •

#10
March 11, 2010 at 09:30:22
Oh, so you did not write this code and do not know how to script?

I reported this post to the Programming Forum so that they will move it to the right place. You will get petter support from them.

Ok, here is some code that will work until another programmer can figure out the math part that stumped me.

::space_ping: Phani
@echo off 

rem You need to initialize your vars at the top to prevent undefined vars.
setLocal EnableDelayedExpansion
set /a totalspace=0
set /a percentused=0
set /a percentfree=0
set /a totalused=0
set /a freespace=0

rem You should initialize the files.
echo Tested Following IPs: > pingLOG.txt
echo Current IP Config: > ipconf.txt
echo Disk Info: > util.txt
echo Disk Space Ussage: > space.txt

rem Call :pinger 
Call :ipconf 
Call :FSutil 
Call :calcpercentages
goto :eof

:calcpercentages
  rem you need code to pars the util.txt
  rem set /a totalspace=!totalused!+!freespace!
  rem set /a percentused=(!totalused!*100)/!totalspace!
  rem set /a percentfree=100-!percentused!
  echo Total space: %totalspace% GB>>space.txt
  echo Free space: %freespace% GB >>space.txt 
  echo Used space: %totalused% GB >>space.txt
goto :eof

:pinger
  for /f "tokens=*" %%I in (IPlist.txt) do ( 
    echo Tested %1 on %TIME% >> pingLOG.txt
    echo Errors Found: >> pingLOG.txt
    ping %%I | find /i "reply" > nul
    if errorlevel 1 echo %%I >> pingLOG.txt
  )
goto :eof

:ipconf
  ipconfig/all >>ipconf.txt
goto :eof

:fsutil
  fsutil volume diskfree c:>util.txt
  for /f "tokens=1-3 delims=:" %%A in (util.txt) do (
    call :SetVar %%B %%A
  )
goto :eof

:SetVar
  echo %1 %5
  if "%5" == "free" set totalused=%1
  if "%5" == "bytes" set totalspace=%1
  if "%5" == "avail" set freespace=%1
goto :eof

:end

This code should answer 3-6 but it uses an IPLIST.TXT file that is pre-populated with the IPs of your servers. This is because this code was written for a different purpose.

as for

1. IP Number of the server
2. Name of the server

This will be hard to do but this should help...

http://www.robvanderwoude.com/ntfor...


Report •

#11
March 11, 2010 at 17:25:56
Using set /a on byte values is probably a no go for 32bit systems as it will max out at 2gb. I have a monster batch script that can be used for these sort of calculations, but it is quite large. There are also others(possibly better) scripts around that do the same:

http://pastebin.com/d9pQZqtp


My real concern here is that if it can't be executed remotely then the whole exercise becomes pointless.

Edit: There is also another issue at hand fsutil requires admin privileges.


Report •

#12
March 11, 2010 at 18:43:28
Based on the script I posted above:

@ECHO OFF
setlocal EnableDelayedExpansion
:fsutil
set n=1
for /f "delims=" %%a in ('fsutil fsinfo drives^|find /v ""') do (
    set drive=%%a
    set drive=!drive:drives: =!
    set list!n!=
    for /f "tokens=2 delims=:" %%b in ('fsutil volume diskfree !drive!^|find /v "ready"') do (
        set huh=%%b
        set huh=!huh:~1,-1!
        for %%c in (!n!) do set list%%c=!list%%c!!huh!,
    )
    if defined list!n! (
        for %%c in (!n!) do set list%%c=!list%%c!!drive!
        set /a n+=1
    )
)
set /a n-=1
for /l %%a in (1 1 !n!) do (
    for /f "tokens=1-4 delims=," %%b in ("!list%%a!") do (
        call bigmath %%d / %%c point
        call bigmath !point! * 100 result
        call bigmath %%c / 1073741824 gb
        echo %%e, !gb!gb, !result!%% free
    )
)

pause


Report •

#13
March 11, 2010 at 20:18:54
Hi omega,

Below are the results

PingLOG.txt
Tested Following IPs:

space.txt
Disk Space Ussage:
Total space: 0 GB
Free space: 0 GB
Used space: 0 GB

ipconf.txt
Windows IP Configuration details
Ethernet adapter Local Area Connection details

util .txt
Disk Info:


Report •

#14
March 11, 2010 at 21:54:40
Here's something that *should* work on a single machine, but I'm sure I've got the ping part of the script wrong:

http://pastebin.com/kACu7U38

Of course the problem is still to remote execute......

I would change "output.txt" to \\computername\share\filename, provided all the machines can access the share.


Report •

#15
March 12, 2010 at 07:16:39
Thanks Judago I was wondering how you delt with large number in scripting,

Phani:
You may have to run the script as administrator if you are using Windows Vista or 7. The "fsutil" will only run under elevated rights.

As for the Pinglog remove the REM on the "Call Pinger" line. I had it remarked out so that it would not take so long to run when I was testing the Disk Info module.

Change this..

rem Call :pinger 
Call :ipconf 
Call :FSutil 
Call :calcpercentages
goto :eof

to this

Call :pinger 
Call :ipconf 
Call :FSutil 
Call :calcpercentages
goto :eof

One other change...

echo Tested %1 on %TIME% >> pingLOG.txt

To...

echo Tested %%I on %TIME% >> pingLOG.txt


Report •


Ask Question