Welcome, guest | Sign In | My Account | Store | Cart

This module , does not take any i/p file. It first does a net view command , and gets the list of computers in the domain. Connects to each computer , performs size check and returns the size of the harddisk. Please note that , the file and dir sizes are caluclated in DOS. Windows performs a sort of approximation. So there will be slight variation in the amount of space reported by DOS and Windows. If I am not wrong DOS gives u the best values.

Python, 49 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
Author : Ratnakar Malla
This module , does not take any i/p file. It first does a net view
command , and gets the list of computers in the domain. Connects
to each computer , performs size check and returns the size of the
harddisk. Please note that , the file and dir sizes are caluclated
in DOS. Windows performs a sort of approximation. So there will be
slight  variation in the amount of space reported by DOS and Windows.
If I am not wrong DOS gives u the best values.
 """
import win32net
import string
import fpformat
import os
import calendar

f=os.popen("net view")
for x in f.readlines():
    if x.startswith("\\"):
        compName=x.split()[0]
        string.replace(compName,"\\","  ")
        cName=string.strip(compName)
        print "Now working on : ",cName
        try:
            win32net.NetUseDel(None,'Z:',0)
            print "Z: Drive already exists, deleting ... "
        except:
            print "Starting .."
        server=cName+ '\\' + 'c$'
       
        try:
            win32net.NetUseAdd(None,1,{'remote': server,'local':'Z:'})
            yr,mon,day,hr,min,sec,jk1,jk2,jk3=calendar.localtime()
            print "Start Time : %s:%s:%s" % (hr,min,sec)
            print "Connected to Z: drive ..."
            myStr=os.popen("dir /s /-C z:\\").readlines()
            print "reading lines into mystr...."
            UsedSpace=fpformat.fix(long(string.split(myStr[-2])[2])/(1000000000.0),2)
            FreeSpace=fpformat.fix(long(string.split(myStr[-1])[2])/(1000000000.0),2)
            TotalSpace=fpformat.fix((float(UsedSpace)+float(FreeSpace)),2)
            print "Used Space:  %s GB" % UsedSpace
            print "Free Space: %s GB" % FreeSpace
            print "Total Space: %s GB" % TotalSpace
            win32net.NetUseDel(None,'Z:',0)
            yr1,mon1,day1,hr1,min1,sec1,jk4,jk5,jk6=calendar.localtime()
            print "End Time : %s:%s:%s" % (hr1,min1,sec1)
        except:
            print "Could not connect to computer %s" % server
fw.close()

I made use of Matt Keranen program,"Windows free drive space report " , and made some modifications. A few days back my boss wanted me to write a prog, so that we can get the Hard Disk Info for each of the computers in the N/w w/o having to log on each computer manually. I tried to use Matt's prog as is , but it requires the Hard Disk Volume info, which unfortunately I could not have it. The only info, I have is the computer Name. I wrote this script , which recursively calls the "dir" DOS command. The "dir" when it finishes gives u the total size of all the files put together and the free space available. Since in a networked environment all the files are on the server, it is not that slow.

2 comments

Mike Nugent 19 years, 3 months ago  # | flag

Why map drive Z:\ ? I am using a Win2000 Professional box and I can type dir /s \remoteserver\c$ and get the directory info. I can also md, rd, del, and copy from my local box to the remote server on the command line. copy c:\somefile.txt \remoteserver\c$\temp works. It is a good script and I will be modifying to work with the 57 Domain Controllers here.

jános juhász 14 years, 10 months ago  # | flag

The same with win32com.client.

import win32com.client as com


def TotalSize(drive):
    """ Return the TotalSize of a shared drive [GB]"""
    try:
        fso = com.Dispatch("Scripting.FileSystemObject")
        drv = fso.GetDrive(drive)
        return drv.TotalSize/2**30
    except:
        return 0

def FreeSpace(drive):
    """ Return the FreeSape of a shared drive [GB]"""
    try:
        fso = com.Dispatch("Scripting.FileSystemObject")
        drv = fso.GetDrive(drive)
        return drv.FreeSpace/2**30
    except:
        return 0

drive = r'\\servername\c$'
print 'TotalSize of %s = %d' % (drive, TotalSize(drive))
print 'FreeSapce on %s = %d' % (drive, FreeSapce(drive))