Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Advanced Files and Folders in WSH
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Advanced Files and Folders in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-02-12

    Table of Contents:
  • Advanced Files and Folders in WSH
  • A Real-World Scenario
  • Examining File Attributes
  • Changing File Attributes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Advanced Files and Folders in WSH - Examining File Attributes


    (Page 3 of 4 )

    The FileSystemObject also allows us to view and edit a file or folder’s properties.  This can be very helpful for error-checking before trying to modify a file or folder since a read-only attribute will result in an error.

    Value

    Attribute

    0

    None

    1

    Read-only

    2

    Hidden

    4

    System

    32

    Archive

    64

    Shortcut

    2048

    Compressed

    The File and Folder objects both have an Attributes property that contains an integer representing the file or folder’s attribute settings.  This integer is obtained by adding the constant value in the table to the left for each attribute that is applied to the file.

    For example, a Read-only System file would have an attribute value of 5 by adding 1 for Read-only and 4 for System.

    Let’s put this to real-world use.  In your root folder is a file called boot.ini.  This is a Windows configuration file that contains the contents of the Advanced Boot Menu.  It typically has Read-only, Hidden, System, and Archive attributes giving it an attribute value of 39.  Take a look at the following code.

    Const NO_ATTRIBUTES = 0

    Const READ_ONLY = 1

    Const HIDDEN = 2

    Const SYSTEM = 4

    Const ARCHIVE = 32

    Const LNK_SHORTCUT = 64

    Const COMPRESSED = 2048

     

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFile = objFso.GetFile("C:boot.ini")

     

    Wscript.Echo "The file " & objFile.Path & " with attribute code " _

       & objFile.Attributes & " has the following attributes:"

     

    If objFile.Attributes AND NO_ATTRIBUTES Then

       Wscript.Echo "No attributes"

    End If   

    If objFile.Attributes AND READ_ONLY Then

       Wscript.Echo "Read-only"

    End If   

    If objFile.Attributes AND HIDDEN Then

       Wscript.Echo "Hidden"

    End If   

    If objFile.Attributes AND SYSTEM Then

       Wscript.Echo "System"

    End If   

    If objFile.Attributes AND ARCHIVE Then

       Wscript.Echo "Archive"

    End If   

    If objFile.Attributes AND LNK_SHORTCUT Then

       Wscript.Echo "Shortcut"

    End If   

    If objFile.Attributes AND COMPRESSED Then

       Wscript.Echo "Compressed"

    End If

    In the beginning of our code we set some constants for readability purposes.  Their values correspond to the attribute values from the table above.  We begin by connecting to the FileSystemObject and getting a handle on our file.

    Then, we Echo the current file information.  Finally, a series of If statements checks to see if each of the attributes exist.  Notice that we check our values from smallest to largest.  The code should have an output similar to the following:

    The file C:boot.ini with attribute code 39 has the following attributes:

    Read-only

    Hidden

    System

    Archive

    More Windows Scripting Articles
    More By Nilpo/Developer Shed Staff Writer


       · It this article we finish up our series by taking a look at the advanced methods...
     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT