keith_kauai has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to find a command in perl to find disk space on a servers d drive and put it into a variable so i may do some calcualtions. The below command does not seem to work. any suggestions? my $ref =`cmd /c DIR \\swmn00xd00073\D$\ | FIND /I "BYTES FREE"`;

Replies are listed 'Best First'.
Re: dir space on remote server
by VinsWorldcom (Prior) on Aug 24, 2009 at 18:42 UTC

    You need to escape your backslashes and your dollar sign:

    my $ref = `cmd.exe /c dir "\\\\swmn00xd00073\\D\$ | FIND /I "BYTES FRE +E"`; print "ref = $ref\n";

    The above worked for me, as shown below:

    {C} > cat script.pl #!/usr/bin/perl use strict; my $ref = `cmd.exe /c dir "\\\\192.168.78.129\\c\$" | find /i "bytes f +ree"`; print "ref = $ref\n"; {C} > script ref = 14 Dir(s) 3,949,535,232 bytes free
      Thank you! That worked! Brilliant!
Re: dir space on remote server
by Marshall (Canon) on Aug 24, 2009 at 18:09 UTC
    I don't know a std Perl way to do this, but for example just a DIR C:\projects on my machine produces as the last line of output: 37 Dir(s) 275,221,610,496 bytes free.

    On Windows, DIR on a volume will yield total bytes free on that volume. You may or may not be able to use all those bytes. You should also be aware that you should NEVER use more than 90%, preferably only 80% or NTFS will "auger into the ground".

Re: dir space on remote server
by imrags (Monk) on Aug 25, 2009 at 03:56 UTC
    There's one more way, since you are using Windows, i suggest you use WMI (Windows Management Instrumentation)
    and use the class "Win32_DiskQuota" or "Win32_LogicalDisk" with Win32::OLE
    TIMTOWTDI
    Raghu