1.

Solve : Determine Total Folder Size?

Answer»

I want to loop through my C:\ drive and determine all folders whose size is greater than 100MB, and OUTPUT their names. I THINK this can be done with by piping dir output into the find command, but I'm having trouble because the output that I need from the result of "dir /s" for each subfolder of C:\ looks like this:

Total Files Listed:
1209 File(s) 1,298,576,686 bytes
542 Dir(s) 14,539,620,352 bytes free

So the parsing is difficult. There may be an easier solution altogether. Can anyone help me out here?

Thanks,
gmWriting recursive batch files leaves a lot to be desired. VBScript makes this a bit easier.

Code: [Select]On Error Resume Next

Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives

For each ds in dc
Select Case ds.DriveType
Case 2
Set RootDir = fso.GetFolder(ds & "\")
GetThePaths(RootDir)
End Select
Next

Function GetThePaths(Folder)
For Each Subfolder in Folder.SubFolders
If Subfolder.Size > 100000000 Then
WScript.Echo "Folder: " & Subfolder.Path & vbTab & "Size: " & Subfolder.Size
End If
GetThePaths Subfolder
Next
End Function

It written to check all your hard drive partitions. Save the script with a vbs extension and run from the command prompt as cscript scriptname.vbs.

Sidewinder: To go a step further, let's say I want to create a webpage with a listing of all the files in a directory along with things like date last updated, author, title, comments and other such file attributes.

Actually looking for a dynamic web page so when new files are added and someone clicks a link to display a "directory" of sorts, it would be dynamic instead of static or having to have someone update a webpage for the directory contents to keep it current.

Is there a listing of attributes that can be displayed from the fso?The FileSystemObject offers the following properties for files:

Attributes Property
DateCreated Property
DateLastAccessed Property
DateLastModified Property
Drive Property
IsRootFolder Property
Name Property (FileSystemObject)
ParentFolder Property
Path Property (FileSystemObject)
ShortName Property
ShortPath Property
Size Property
SubFolders Property
Type Property

The other information you requested is metadata which can be extracted by querying the BuiltInProperties of each application.

Title
Subject
Author
Keywords
Comments
Template
Last author
Revision number
Application name
Last print date
Creation date
Last save TIME
Total editing time
Number of pages
Number of words
Number of characters
Security
Category
Format
Manager
Company
Number of bytes
Number of lines
Number of paragraphs
Number of slides
Number of notes
Number of hidden Slides
Number of multimedia clips
Hyperlink base
Number of characters (with spaces)

Sidewinder,

Very cool. I wasn't aware you could use vbscript like this.....

Now I have one additional problem. My original post was a sort of simplified version of the full problem I'm tackling. The other pieces of the problem I already knew how to solve (in DOS, of course), but I don't know how to solve them in vbscript. For example....

Sometimes I need to determine if a file exists
Sometimes I need to determine if a process is running (didnt know how to do this one in DOS actually)

I was also curious about why the syntax calling GetThePath was different when it was used recursively within itself (no parentheses in that case). Do you know of a good vbscript tutorial with examples of these kinds of file system tasks....

Thanks for your help,
gmThe syntax of calling the GetThePath function actually calls for using the parentheses. I have a large closet of code snippets that I use to create workable scripts. Seems the coding style doesn't match across all of them! In any case it will work both ways and if the interpreter doesn't care neither do I.

To check if a file exists, create an INSTANCE of the FileSystemObject and use the FileExists Method:
Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("c:\windows\system32\cmd.exe") Then
' your code goes here
End If

To determine if a specific process is executing this is one way to find out:
Code: [Select]strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = 'Notepad.exe'")

If colProcesses.Count > 0 Then
WScript.Echo "Notepad Is Executing"
Else
WScript.Echo "Notepad Is Not Executing"
End If

The two examples can be done in batch, naturally the syntax is very different! As with any language, there is more than one way to do the same thing.

I ALWAYS send users to the Script Center where you'll find a collection of samples, tools and articles explaining VBScript from the ground up.

Good luck. Thanks again Sidewinder...

Should be very useful.SW,

Thanks for the time on the fso properties. I likely posted it in the wrong forum, but it was a natural add-on to gm's question and your response. Thinking of a simple content management/article archive browser-based little diddy. I will play around with the above. Thanks also for the resource. By the way, is there a standard for metadata. Like you say, each app is different; that makes it difficult to create a dynamic browser page I would think.
You can use tasklist to find if a process is running, by piping its output through FINDSTR and using the && or || operators to check if it was successful (or unsuccessful)

I expect you know about the && and || operators. The || is two pipe symbols

The /I switch for findstr makes it case insensitive

If the operation to the left of the && is successful, the operation to the right of the && is performed.

Code: [Select]tasklist | findstr /I "notepad.exe" && echo notepad is running

If the operation to the left of the || is UNsuccessful, the operation to the right of the || is performed.

Code: [Select]tasklist | findstr /I "notepad.exe" || echo notepad is not running

Redirect to the null device if you do not want the output line of tasklist to show up.

Code: [Select]tasklist | findstr /I "notepad.exe"> nul && echo notepad is running

Of course you could be performing some other action than a simple echo, eg goto a label, or whatever, and you can use braces for multiple lines

Code: [Select]tasklist | findstr /I "notepad.exe"> nul && (
echo notepad
echo is
echo running
)

You can see this is like a block IF...ENDIF in BASIC

(I like to indent braced lines in this type of situation and also in FOR loops to make structure clearer)

Quote from: contrex on April 21, 2007, 04:08:45 AM

Code: [Select]tasklist | findstr /I "notepad.exe" && echo notepad is running
just for info, tasklist can find whether notepad.exe is running without findstr
Code: [Select]tasklist /FI "IMAGENAME eq notepad.exe" /NH


Discussion

No Comment Found