# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
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.
0
Commented:
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.
0
Author Commented:
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
0
Author Commented:
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
0
OwnerCommented:
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.
0
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.
0
OwnerCommented:
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.
0
OwnerCommented:
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?
0
Author Commented:
I am looking to automate so script would be useful to analyze. Thanks
0
Owner/OperatorCommented:
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
0
Commented:
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
0
Author Commented:
It would be better if it would be a script, either in VB or Powershell, thanks
0
OwnerCommented:
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.
0
Author Commented:
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
0
OwnerCommented:
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.
0
Commented:
I'll work up something in VBS that behaves the way you described above...

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

~bp
0
Author Commented:
Thank you bp
0
Commented:
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
0
Author Commented:
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
0
Commented:
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

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

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

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

' 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

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
End Function

' Left pad a string to any length with a specified character
End Function

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

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

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

Error.JPG
0
Commented:
Sounds like you may have just clicked on it to run it.  You will need to run it from a command prompt window, or in a BAT file, with the following command line:

cscript //nologo ee28700113.vbs D:\Test

if you want to save the output to a file then you can use standard redirection for that as follows:

cscript //nologo ee28700113.vbs D:\Test > report.txt

~bp
3

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks bp working fine
0
Commented:

~bp
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.