Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - An Introduction to Files and Folders in WS...
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

An Introduction to Files and Folders in WSH
By: Nilpo/Developer Shed Staff Writer
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-02-05

    Table of Contents:
  • An Introduction to Files and Folders in WSH
  • Enumerating Files and Folders
  • Working with Files and Folders
  • Using the Sub-Objects
  • Using What You’ve Learned

  • 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


    An Introduction to Files and Folders in WSH - Using the Sub-Objects


    (Page 4 of 5 )

    There is a second way to accomplish this same task.  In the last code example, we used just the methods and properties of the FileSystemObject.  However, as we’ve seen, the FileSystemObject also contains File and Folder objects.  Each of these objects also contains methods and properties that we can use.

    The File and Folder Objects

     

    Shared Methods

    object.Copy Destination[, Overwrite]

    object.Delete [Force]

    object.Move Destination

     

    Shared Properties

    object.Attributes [= newattributes]

    object.DateCreated

    object.DateLastAccessed

    object.DateLastModified

    object.Drive

    object.Name

    object.ParentFolder

    object.Path

    object.ShortName

    object.ShortPath

    object.Type

     

    Folder Methods

    object.CreateTextFile(Filename[, Overwrite[, Unicode]]))

     

    File Methods

    object.OpenAsTextStream([Iomode [, Format]])

     

    Folder Properties

    object.Files

    object.IsRootFolder

    object.Size

    object.SubFolders

    Many of the methods and properties are shared by both the File object and the Folder object.  Most of these methods and properties are self explanatory so I’m not going into too much detail in an effort to save space.

    Each of these methods and properties returns a string, an object, or a collection with the exception of Copy, Delete, and Move.

    However, there are a couple of points I want to make.  First, the Attributes property will either get or change the attributes of a file or folder based on whether or not a parameter is supplied.  You should also note that the ParentFolder property returns an object.  (It actually performs a GetFolder on the parent folder).  The OpenAsTextStream method also returns an object.  And finally, both the Files property and the SubFolders property return collections.

    Keeping these things in mind, let’s rewrite our last code example.  This time we’ll create a third folder and move the files from the second to the third.  Then we’ll delete the empty folder.  To make things interesting, I’m only going to reference one file from the second folder.  This will give us a chance to see some of the other properties in action.  Obviously, I wouldn’t do this in a real world situation.

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFolder3 = objFso.CreateFolder("C:TestFolder3")

    Set objFile = objFso.GetFile("C:TestFolder2test1.txt")

    Set objFolder2 = objFile.ParentFolder

     

    For Each file In objFolder2.Files

       file.Move objFolder3.Path & "" & file.Name

    Next

     

    objFolder2.Delete

    At this point, you should be able to see exactly what this code is doing.  First we create our new folder.  Next, we use the GetFile method to get a handle on our file.  Then we use the ParentFolder property to get a handle on its parent folder.  Remember to use the Set statement since the ParentFolder returns an actual object and not a string.

    Once we have a handle on our second folder, set up a loop to grab each file and move it to the new folder.  We finish up by simply deleting the second folder.

    If you’ve been following along, at this point you should have two folders, TestFolder and TestFolder3, both containing the exact same files.

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


       · VBScript's FileSystemObject provides a very useful interface for accessing files and...
     

    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