Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse WMI to make a folder compress its contents to save space in Visual Basic 2005
DescriptionThis example shows how to use WMI to make a folder compress its contents to save space in Visual Basic 2005.
Keywordscompress, folder, compress folder, compressed folder, compression, Visual Basic 2005
CategoriesAlgorithms, Miscellany, Files and Directories
 
When you click the Compress button, the following code executes. It creates the WMI service object and executes a query similar to:

    Select * From Win32_Directory Where Name = 'C:\\whatever\\dir'

Note that the query uses doubled back slashes because WMI scripts use back slashes as special characters.

The code loops through the returned folders (there should be one) and calls its Compress method to make the folder compress its contents.

 
Private Sub btnCompress_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnCompress.Click
    Dim wmi_service As Object = _
        GetObject("winmgmts:\\.\root\cimv2")
    Dim query As String = _
        "Select * From Win32_Directory Where Name = '" & _
        txtFolder.Text & "'"
    query = query.Replace("\", "\\")
    Dim folders As Object = wmi_service.ExecQuery(query)
    Debug.WriteLine(query)

    For Each objFolder As Object In folders
        Debug.WriteLine(objFolder.name)
        objFolder.Compress()
    Next objFolder

    MessageBox.Show("Done", "Done", MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated