Link to home
Start Free TrialLog in
Avatar of jmohan0302
jmohan0302

asked on

Script to get folder details

I am looking for a script that has to provide that has to list the contents of the folder and file along with the its size (in GB) that includes it subdirectory as well in windows
Avatar of BillDL
BillDL
Flag of United Kingdom of Great Britain and Northern Ireland image

jmohan0302

If you are still looking for a suggestion, then there are a few things that are important to know.

1. Is this just for one computer or do you need to do this for more than one computer on a network?
2. What is / are the operating system(s)?
3. Are you able to use a 3rd-party utility program to do this?
4. Would it be possible to provide us with a basic example of the kind of layout and details you want in the results?

It is not really clear from your question how you want the results to be displayed.
do you have a sample on how the output should look like? in plain text file, html, excel, etc? I'm sure experts here manage to provide you some suggestions/solutions if you provide us more details.
Avatar of jmohan0302
jmohan0302

ASKER

Hi Ryan,

I am looking for the output to be like this, say for ex: I want to find the size of the folder D:\Test which has many subfolders and files it . I would like to display the output as the total size of D:\Test in GB along with its contents and size and files size as well..
PATH                      No. of Subdirectories            No. Of Files             Size(GB)
D:\Test                     2                                                  1                            45

Listing contents of D:\ Test
Test1               25 GB    
Test2                20 GB
Test.txt             1kb

PATH                      No. of Subdirectories            No. Of Files             Size(GB)

D:\Test\Test1          1                                                   0                            25
PATH                      No. of Subdirectories            No. Of Files             Size(GB)
D:\Test\Test1          0                                                   2                             20

Listing contents of D:\ Test\Test1
Pdf              25 GB

Listing Contents of D:\Test\Test1

Is what like I am looking for? Else I would just like to popup the contents but I need the output in GB. Thanks
With respect to the above output, it would be better if the output sorts the folder contents based on the biggest size and the file which is of more size in red colour and descends with next biggest folder and the biggest size in that folder...Thanks
Can I get you to think some more about how you want this script to work?

1. As you've laid it out, the output is going to include multiple lines for every subfolder and at least one line for every file; if, as you suggested, there are many subfolders and files, the output is going to be voluminous. You might therefore wish to place a restriction on how far down the subfolder tree the script descends or a size threshold below which items will be summarized rather then listed in detail.

2. I'm not sure what your purpose is, but when I use this sort of function I'm looking for space hogs. Sorting the output so the largest items appear first would support this use.

3. There are any number of 3rd party, including free solutions to this problem that provide a graphical interface representing disk space allocation which you can drill down into to look at more specific items. I still use Zero Assumption Disk Space Visualizer, despite some problems it has, but there are others.
What it sounds like you are looking for is something equivalent to the du function from Linux. As far as gui tools I recommend one called windirstat.
It takes time to compose an answer, so I didn't see your further clarification until I posted. You've already found my point 2 on your own.

I'll continue to suggest that getting this functionality from someone else's program is going to be preferable to writing it from scratch. One that a lot of people seem to like is WinDirStat.
Lots of people awake at this hour, I see. And I type sooooo sloooowly. Rather than list a half dozen more disk analyzers, let me ask: are you intending to perhaps automate the handling of disk space hogs by archiving them off to slower storage or at least deduplicating them?
I am looking to automate so script would be useful to analyze. Thanks
This is a tiny free program I've used since Win95.  It was written by Karen Kenworthy.  It will provide the information you seek.  http://www.majorgeeks.com/files/details/karens_directory_printer.html
Since 3rd party tools were mentioned, one I use is SearchMyFiles, by NirSoft.

* It can find files and folders by:
- Creation, modified, last access dates.
- Multiple file or folder names. Wildcards allowed
- File sizes

* You can export results to .txt, csv, html, xml
* There's nothing to install. You can run it on a USB.
* Free
It would be better if it would be a script, either in VB or Powershell, thanks
Here's a possible script:

http://robwillis.info/2014/07/powershell-script-find-the-25-largest-files-and-list-folder-sizes-for-any-drive/

You could use it as a starting point. It demonstrates a couple of important features: privilege elevation so the script can scan the whole drive, recursive Get-ChildItem for both files and folders, reporting in MB (you wanted GB, but it should be clear where to change it), limits on the volume of output, ....

It's still rather far from what you were asking for, but it's closer than anything I'm going to write in the next week or month.
Getting the following error, when executing the script:

Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
At D:\Scripts\EE\25.ps1:34 char:39
+ Remove-Item $findFilesFoldersOutput 2& <<<< gt; $null
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

Thanks
Try downloading from the link given for lowDiskSpace.txt at Rob's website, then rename the suffix to .ps1 - it seems that the displayed text contains HMTL-encodings (such as &gt;) that may be a messed-up result of cut'n'paste. The .txt download does not have these.
I'll work up something in VBS that behaves the way you described above...

~bp
Thanks bp
Will adjust something I have that is close to this in about 9 hours...

~bp
Thank you bp
So, If you have a set of subfolders like this:

D:\Test\
..D1
....D1A
....D1B
..D2
....D2A
....D2B

Do you want to see them listed in this order:

D:\Test
D:\Test\D1
D:\Test\D2
D:\Test\D1\D1A
D:\Test\D1\D1B
D:\Test\D2\D2A
D:\Test\D2\D2B


Or this order:

D:\Test
D:\Test\D1
D:\Test\D1\D1A
D:\Test\D1\D1B
D:\Test\D2
D:\Test\D2\D2A
D:\Test\D2\D2B


~bp
I would prefer the output to be:

Contents of D:\Test                                   Size                        Type

D:\ Test                                                        25 GB                    Directory

D1                                                                 10 GB                   Directory
D2                                                                 15 GB                   Directory

Contents of D:\Test                                    Size                        Type
D1A                                                               4.5 GB                    Directory
D1A.txt                                                          500 MB                  file
D1B                                                                4.5 GB                   Directory
D1B.txt                                                           500 MB                File

Also I am looking the output to sort the contents from biggest size folder to least size folder.  The script should also accept if the input is even the root directory like C:\ . It would be better if the script accepts the input at the runtime.

Thanks
Okay, before I go too much further on this, I'd like to know if this is what you are looking for.  You seem to be changing the desired output a bit along the way.  I haven't added in any sorting of the folders and files by size yet, but that will take a bit of work, so don't want to invest in that if this isn't what you want.  I created some test data that looked like this:

Here is the general TREE of the files and folders:
B:\ee\EE28700113>tree /a /f Test

B:\EE\EE28700113\TEST
|   f0-2.txt
|   f0-1.txt
|   f0-3.txt
|
+---D2
|   |   f2-3.txt
|   |   f2-2.txt
|   |   f2-1.txt
|   |
|   +---D2A
|   |       f2A-4.txt
|   |       f2A-1.txt
|   |       f2A-2.txt
|   |       f2A-3.txt
|   |
|   \---D2B
|           f2B-2.txt
|           f2B-1.txt
|
\---D1
    |   f1-1.txt
    |
    +---D1B
    |       f1B-4.txt
    |       f1B-1.txt
    |       f1B-2.txt
    |       f1B-5.txt
    |       f1B-3.txt
    |
    \---D1A
            f1A-1.txt

Open in new window

And here is the DIR output:
B:\ee\EE28700113>dir /s Test

 Directory of B:\ee\EE28700113\Test

07/28/2015  03:42 PM    <DIR>          .
07/28/2015  03:35 PM    <DIR>          ..
07/28/2015  03:36 PM    <DIR>          D2
07/28/2015  03:41 PM             2,000 f0-2.txt
07/28/2015  03:41 PM             1,000 f0-1.txt
07/28/2015  03:36 PM    <DIR>          D1
07/28/2015  03:42 PM             3,000 f0-3.txt
               3 File(s)          6,000 bytes

 Directory of B:\ee\EE28700113\Test\D2

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:42 PM    <DIR>          ..
07/28/2015  03:36 PM    <DIR>          D2A
07/28/2015  03:36 PM        30,000,000 f2-3.txt
07/28/2015  03:36 PM    <DIR>          D2B
07/28/2015  03:36 PM        20,000,000 f2-2.txt
07/28/2015  03:36 PM        10,000,000 f2-1.txt
               3 File(s)     60,000,000 bytes

 Directory of B:\ee\EE28700113\Test\D2\D2A

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:36 PM    <DIR>          ..
07/28/2015  03:36 PM     4,000,000,000 f2A-4.txt
07/28/2015  03:36 PM     1,000,000,000 f2A-1.txt
07/28/2015  03:36 PM     2,000,000,000 f2A-2.txt
07/28/2015  03:36 PM     3,000,000,000 f2A-3.txt
               4 File(s) 10,000,000,000 bytes

 Directory of B:\ee\EE28700113\Test\D2\D2B

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:36 PM    <DIR>          ..
07/28/2015  03:36 PM     6,000,000,000 f2B-2.txt
07/28/2015  03:36 PM     5,000,000,000 f2B-1.txt
               2 File(s) 11,000,000,000 bytes

 Directory of B:\ee\EE28700113\Test\D1

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:42 PM    <DIR>          ..
07/28/2015  03:36 PM    <DIR>          D1B
07/28/2015  03:36 PM    <DIR>          D1A
07/28/2015  03:36 PM               100 f1-1.txt
               1 File(s)            100 bytes

 Directory of B:\ee\EE28700113\Test\D1\D1B

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:36 PM    <DIR>          ..
07/28/2015  03:36 PM           500,000 f1B-4.txt
07/28/2015  03:36 PM           200,000 f1B-1.txt
07/28/2015  03:36 PM           300,000 f1B-2.txt
07/28/2015  03:36 PM           600,000 f1B-5.txt
07/28/2015  03:36 PM           400,000 f1B-3.txt
               5 File(s)      2,000,000 bytes

 Directory of B:\ee\EE28700113\Test\D1\D1A

07/28/2015  03:36 PM    <DIR>          .
07/28/2015  03:36 PM    <DIR>          ..
07/28/2015  03:36 PM           100,000 f1A-1.txt
               1 File(s)        100,000 bytes

     Total Files Listed:
              19 File(s) 21,062,106,100 bytes
              20 Dir(s)  4,149,273,858,048 bytes free

Open in new window

Here is the output from my draft of a VBS script to meet your needs (the "*" before the filename highlights folders, which are also listed before files):
B:\ee\EE28700113>cscript //nologo EE28700113.vbs Test

Listing contents of B:\ee\EE28700113\Test

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
           2            3     19.61 GB *D2
           2            1         2 MB *D1
                               1.95 KB  f0-2.txt
                               1000     f0-1.txt
                               2.93 KB  f0-3.txt

Listing contents of B:\ee\EE28700113\Test\D2

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
           0            4      9.31 GB *D2A
           0            2     10.24 GB *D2B
                              28.61 MB  f2-3.txt
                              19.07 MB  f2-2.txt
                               9.54 MB  f2-1.txt

Listing contents of B:\ee\EE28700113\Test\D2\D2A

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
                               3.73 GB  f2A-4.txt
                             953.67 MB  f2A-1.txt
                               1.86 GB  f2A-2.txt
                               2.79 GB  f2A-3.txt

Listing contents of B:\ee\EE28700113\Test\D2\D2B

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
                               5.59 GB  f2B-2.txt
                               4.66 GB  f2B-1.txt

Listing contents of B:\ee\EE28700113\Test\D1

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
           0            5      1.91 MB *D1B
           0            1     97.66 KB *D1A
                                100     f1-1.txt

Listing contents of B:\ee\EE28700113\Test\D1\D1B

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
                             488.28 KB  f1B-4.txt
                             195.31 KB  f1B-1.txt
                             292.97 KB  f1B-2.txt
                             585.94 KB  f1B-5.txt
                             390.62 KB  f1B-3.txt

Listing contents of B:\ee\EE28700113\Test\D1\D1A

  SUBFOLDERS        FILES         SIZE  FILENAME
------------ ------------ ------------  ----------------------------------------
                              97.66 KB  f1A-1.txt

Open in new window

And here is the script:
' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get folder name to list off the command line, make sure it's valid
If (WScript.Arguments.Count > 0) Then
    strFolder = Wscript.Arguments(0)
    If Not objFSO.FolderExists(strFolder) Then
        WScript.StdErr.WriteLine "Specified folder does not exist."
        WScript.Quit
    End If
Else
    WScript.StdErr.WriteLine "No folder name specified to list."
    WScript.Quit
End If

' Look for files
DoFolder objFSO.GetFolder(strFolder)

Sub DoFolder(objFolder)
   On Error Resume Next

   If Err.Number = 0 Then
      ' Parent folder
      PrintFolderLine objFolder.Path
'      PrintFolderLine(objFolder.Path) & "," & objFolder.Files.Count & "," & objFolder.SubFolders.Count & "," & objFolder.Size

      PrintHeader

      ' List all folders first
      For Each objSubFolder In objFolder.SubFolders
         On Error Resume Next
         PrintDirLine objSubFolder.Name, FormatSize(objSubFolder.Size), objSubFolder.SubFolders.Count, objSubFolder.Files.Count
         If Err.Number <> 0 Then 
            PrintDirLine objSubFolder.Name, "N/A", "N/A""N/A", "N/A"
         End If
         On Error Goto 0
      Next

      ' List files
      For Each objFile In objFolder.Files
         PrintFileLine objFile.Name, FormatSize(objFile.Size)
      Next

      ' Recursively drill down into subfolder
      For Each objSubFolder In objFolder.SubFolders
         On Error Resume Next
         DoFolder objSubFolder
      Next
   Else
      PrintFolderLine "N/A"
   End If

   On Error Goto 0

End Sub

Sub PrintHeader()
   Wscript.Echo LPad("SUBFOLDERS", 12, " ") & " " & LPad("FILES", 12, " ") & " " & LPad("SIZE", 12, " ") & "  " & "FILENAME"
   Wscript.Echo String(12, "-") & " " & String(12, "-") & " " & String(12, "-") & "  " & String(40, "-")
End Sub

Sub PrintFileLine(strName, strSize)
   Wscript.Echo String(12, " ") & " " & String(12, " ") & " " & LPad(strSize, 12, " ") & "  " & strName
End Sub

Sub PrintDirLine(strName, strSize, strFolders, strFiles)
   Wscript.Echo LPad(strFolders, 12, " ") & " " & LPad(strFiles, 12, " ") & " " & LPad(strSize, 12, " ") & " *" & strName
End Sub

Sub PrintFolderLine(strFolder)
   Wscript.Echo vbNewLine & "Listing contents of " & strFolder & vbNewLine
End Sub

' Function to format a number into typical size scales
Function FormatSize(intSize)
   arrLabel = Array("  ", "KB", "MB", "GB", "TB")
   For i = 0 to 4
      If intSize > 1024 Then 
         intSize = intSize / 1024
      Else
         Exit For 
      End If
   Next
   FormatSize = Round(intSize, 2) & " " & arrLabel(i)
End Function

' Add surrounding double quotes to a string
Function Quote(s)
   Quote = Chr(34) & s & Chr(34)
End Function

' Right pad a string to any length with a specified character
Function RPad(strText, intLen, chrPad)
  RPad = Left(strText & String(intLen, chrPad), intLen)
End Function

' Left pad a string to any length with a specified character
Function LPad(strText, intLen, chrPad)
  LPad = Right(String(intLen, chrPad) & strText, intLen)
End Function

Open in new window

Let me know if this would work, or what questions or changes you have.

~bp
Thanks bp, I am getting the below error when executing,:

Line 12, CHAR 5
Error: The handle is invalid
Code: 80070006
Source: Null

I've attached for your  reference.
Error.JPG
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks bp working fine
Welcome, glad that was useful.

~bp