Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - 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 - Working with Files and Folders


    (Page 3 of 5 )

    Okay, enumerating files and folders is all well and good, but let’s do something a little more useful.  Chances are that if you are scripting files and folders you are most likely interested in performing actions on them.  Common actions include moving, copying, and deleting.  So let’s start there.  As you probably expect, the FileSystemObject provides methods for doing all of these and more.

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

    object.CreateFolder Foldername

    object.DeleteFile File[, Force]

    object.DeleteFolder Folder[, Force]

     

    object.CopyFile Source, Destination[, Overwrite]

    object.CopyFolder Source, Destination[, Overwrite]

    object.MoveFile Source, Destination

    object.MoveFolder Source, Destination

     

    object.FileExists

    object.FolderExists

    We’re going to create a basic script that will create a folder and a add a couple of text files to it so that we have some files to work with.

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objFolder = objFso.CreateFolder("C:TestFolder")

     

    For x = 1 to 5

       strName = "test" & x & ".txt"

       objFolder.CreateTextFile strName

    Next

    Now you should have a folder at C:TestFolder that has five text files named test1.txt through test5.txt.  Now we’ll create a second folder called TestFolder2 and copy our text files to that location.

    Set objFso = CreateObject("Scripting.FileSystemObject")

    Set objSrc = objFso.GetFolder("C:TestFolder")

    Set objDest = objFso.CreateFolder("C:TestFolder2")

     

    Set colFiles = objSrc.Files

    For each file In colFiles

       objFso.CopyFile file.Path, objDest.Path & "" & file.Name

    Next

    I’ve taken a little roundabout way of doing this so that I could demonstrate a few more properties and methods.  You can see that I’ve constructed my file names and paths completely using the properties available to me.  Just remember that all of these methods accept either objects or strings as their parameters and construct your code accordingly.

    In this case, CopyFile accepts two string parameters.  The first is the full path of our source file to copy, and the second is the full path to the destination.  We pass our source with file.path.  The path property returns a full path as a string so we can use that for our first parameter.

    To avoid some confusion, this is what our properties actually return:
    file.Path returns “C:\test.txt”
    objDest.Path returns “C:\TestFolder2”
    file.Name returns “text.txt”

    The second parameter requires a full path to the destination file.  Since we just created that folder, we need to construct a string to use.  So we use the objDest.Path and file.Name properties.  We’ve just concatenated them.  Don’t forget to add the extra backslash (\) because objDest.Path does not return a trailing slash on our folder.

    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 6 hosted by Hostway
    Stay green...Green IT