Directory size on disk

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • idoha
    New Member
    • Aug 2007
    • 12

    Directory size on disk

    Hello,
    I wish to find out the total space (on disk) of a given directory \ folder on windows platform.
    Does anybody know how to do it ?

    Thanks,
    Ido.
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    What have you tried so far? Please post the code you have used to solve this and we can assist from there.

    Just a tip though, you may want to search CPAN for modules to do what you want. If you go to search.cpan.org and search for "size", you will see an entry for File::Size which will return the size of files and directories.

    Regards,

    Jeff

    Comment

    • idoha
      New Member
      • Aug 2007
      • 12

      #3
      Hello Jeff,
      Thanks for your reply.

      I used stat function which returns several parameters regarding a given file path, one of which holds the file size. But unfortunately this value is only the actual size of file, not the space it occupies on the disk. I need "size on disk" value.

      I have been trying to use WMI as well, but it doesn't contain any "size" values on directories, just on files (and this is the actual size as well).

      One more thing, I am trying to avoid a case of manually drill down into the folder and accumulate all its sub directory's files size (presuming I will be able to find out their "size on disk" values).

      Thanks anyway,
      Ido.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Ok, have you tried the File::Size module that I mentioned in my last post? Also, for dealing with file size, try the File::Util module as it returns the file size in bytes, which should be size on disk as you need.

        It just takes a bit of searching on CPAN. You want to deal with files? Search for "file" or "size" and see what comes up. A brief description of what each module does IS given for your review.

        Regards,

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          File::Size is an unauthorized release, which I am not sure what that means, but it may still work OK, I have never tried using it.

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            personally, I might do it like this:

            Code:
            chdir('c:/');
            my @dirinfo = qx|dir c:\windows /S|;
            print @dirinfo[-2,-1];
            and just parse out the size info.

            Note: in the qx|| string I had to use a backslash in front of windows: c:\windows. While Windows allows you to use forward or backslashes in directory paths, DOS does not. Forward slashes are option switches in DOS commands.

            As far as finding out the actual disk blocks a file is using, versus it's file size, I do not know how to do that. You may have to use a third party application that can figure that out.

            Comment

            • idoha
              New Member
              • Aug 2007
              • 12

              #7
              Hi Again,

              I have got 1 major constraint: I cannot use an external package such as File::size since I cannot demand from the customer I am doing it for to install that package either. Therefore, I am searching for a build-in method for that purpose.

              Thanks,
              Ido.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                If this is being written specific for windows, the client is most likely using activestate perl. Activestate perl comes with many Win32 modules in it's standard distribution. You can look into using those modules instead of sticking with regular perl core modules if that is the case. Otherwise, File::Find or your own custom function is the only way to go that I know of, or use the operating system as I showed above if that will work.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  after a little research, the /V switch (verbose mode, a forward slash followed by an upper case V) seems to do what you want, it returns info like this

                  Code:
                  Directory of C:\Perl
                  File Name         Size        Allocated      Modified      Accessed  Attrib
                  
                  
                  .              <DIR>                      12-06-02 10:01p  12-06-02      D      .
                  ..             <DIR>                      12-06-02 10:01p  12-06-02      D      ..
                  SITE           <DIR>                      12-06-02 10:01p  12-06-02      D      site
                  LIB            <DIR>                      12-06-02 10:01p  12-06-02      D      lib
                  EG             <DIR>                      12-06-02 10:01p  12-06-02      D      eg
                  BIN            <DIR>                      12-06-02 10:01p  12-06-02      D      bin
                  HTML           <DIR>                      12-06-02 10:01p  12-06-02      D      html
                  POD2HTMI X~~        16,683        32,768  12-06-02 10:02p  05-31-07       A     pod2htmi.x~~
                  POD2HTMD X~~        34,146        49,152  12-06-02 10:02p  05-31-07       A     pod2htmd.x~~
                  DOCS           <DIR>                      12-06-02 10:08p  12-06-02      D      Docs
                  PDK            <DIR>                      12-06-02 10:08p  12-06-02      D      PDK
                           2 file(s)         50,829 bytes
                           9 dir(s)          81,920 bytes allocated
                  so you can get the allocated disk space as well as the file size. The /S switch will drill down into all the sub directories so at the very end you get the totas all summed up:

                  Code:
                  Total files listed:
                       4,739 file(s)     69,754,995 bytes
                       2,174 dir(s)     125,861,888 bytes allocated
                                           6,290.38 MB free
                                          19,459.84 MB total disk space,  67% in use

                  Comment

                  • miller
                    Recognized Expert Top Contributor
                    • Oct 2006
                    • 1086

                    #10
                    Originally posted by idoha
                    I have got 1 major constraint: I cannot use an external package such as File::size since I cannot demand from the customer I am doing it for to install that package either. Therefore, I am searching for a build-in method for that purpose.
                    At the very least then, just look at the source of File::Size in order to determine how they implemented this. File::Find is a core module though, so as Kevin says, you can just roll your own function as well:

                    [CODE=perl]
                    use File::Find;

                    my $directory = shift || '.';

                    print dir_size($direc tory);

                    sub dir_size {
                    my $directory = shift;
                    die "Directory expected as parameter" if !-d $directory;

                    my $size_total = 0;

                    find({follow => 0, wanted => sub {
                    $size_total += -s $File::Find::na me || 0;
                    }}, $directory);

                    return $size_total;
                    }

                    1;

                    __END__
                    [/CODE]

                    - Miller

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      That File::Util module Jeff mentioned looks interesting.

                      Comment

                      • idoha
                        New Member
                        • Aug 2007
                        • 12

                        #12
                        Originally posted by miller
                        At the very least then, just look at the source of File::Size in order to determine how they implemented this. File::Find is a core module though, so as Kevin says, you can just roll your own function as well:

                        [CODE=perl]
                        use File::Find;

                        my $directory = shift || '.';

                        print dir_size($direc tory);

                        sub dir_size {
                        my $directory = shift;
                        die "Directory expected as parameter" if !-d $directory;

                        my $size_total = 0;

                        find({follow => 0, wanted => sub {
                        $size_total += -s $File::Find::na me || 0;
                        }}, $directory);

                        return $size_total;
                        }

                        1;

                        __END__
                        [/CODE]

                        - Miller
                        Hi Miller,
                        This procedure works ok. The only problem is that it return the actual size of directory, not the total space it occupies on the disk.

                        Comment

                        • idoha
                          New Member
                          • Aug 2007
                          • 12

                          #13
                          Originally posted by KevinADC
                          after a little research, the /V switch (verbose mode, a forward slash followed by an upper case V) seems to do what you want, it returns info like this

                          Code:
                          Directory of C:\Perl
                          File Name         Size        Allocated      Modified      Accessed  Attrib
                          
                          
                          .              <DIR>                      12-06-02 10:01p  12-06-02      D      .
                          ..             <DIR>                      12-06-02 10:01p  12-06-02      D      ..
                          SITE           <DIR>                      12-06-02 10:01p  12-06-02      D      site
                          LIB            <DIR>                      12-06-02 10:01p  12-06-02      D      lib
                          EG             <DIR>                      12-06-02 10:01p  12-06-02      D      eg
                          BIN            <DIR>                      12-06-02 10:01p  12-06-02      D      bin
                          HTML           <DIR>                      12-06-02 10:01p  12-06-02      D      html
                          POD2HTMI X~~        16,683        32,768  12-06-02 10:02p  05-31-07       A     pod2htmi.x~~
                          POD2HTMD X~~        34,146        49,152  12-06-02 10:02p  05-31-07       A     pod2htmd.x~~
                          DOCS           <DIR>                      12-06-02 10:08p  12-06-02      D      Docs
                          PDK            <DIR>                      12-06-02 10:08p  12-06-02      D      PDK
                                   2 file(s)         50,829 bytes
                                   9 dir(s)          81,920 bytes allocated
                          so you can get the allocated disk space as well as the file size. The /S switch will drill down into all the sub directories so at the very end you get the totas all summed up:

                          Code:
                          Total files listed:
                               4,739 file(s)     69,754,995 bytes
                               2,174 dir(s)     125,861,888 bytes allocated
                                                   6,290.38 MB free
                                                  19,459.84 MB total disk space,  67% in use

                          Hi Kevin,
                          This could be very helpful for me. I don't really understand how you run this verbose mode. Do you run it on on "dir" command (dir /V ?).
                          I have been trying to do it and there ain't such an option in XP cmd (only in older version of windows such as 98 and 95).

                          Any idea?

                          Thanks,
                          Ido.

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            go to the DOS prompt and type:

                            dir /?

                            It might be the /n switch with windows XP:

                            /n : Displays a long list format with file names on the far right of the screen.

                            windows XP DOS commands

                            You can try stat()[12] on a file and see if it returns a value. It is the allocated disk space but I don't think it works for Windows:

                            Code:
                            $allocated = (stat('c:\filename.txt'))[12];

                            Comment

                            • idoha
                              New Member
                              • Aug 2007
                              • 12

                              #15
                              Originally posted by KevinADC
                              go to the DOS prompt and type:

                              dir /?

                              It might be the /n switch with windows XP:

                              /n : Displays a long list format with file names on the far right of the screen.

                              windows XP DOS commands

                              You can try stat()[12] on a file and see if it returns a value. It is the allocated disk space but I don't think it works for Windows:

                              Code:
                              $allocated = (stat('c:\filename.txt'))[12];
                              Hi,
                              /n doesn't make any change.
                              Stat doens't return any allocated blocks as it should.

                              No cure for me.

                              Any other suggestions ?

                              Ido.

                              Comment

                              Working...