Moving, Copying, and Deleting Files

The fso object model provides two methods each for moving, copying, and deleting files:

To run the following example, you must create directories named \tmp and \temp in the root directory of drive C. The example performs the following actions:

  1. Creates a text file in the root directory of drive C.

  2. Writes information to the file.

  3. Moves the file to a directory named \tmp.

  4. Copies the file to a directory named \temp.

  5. Deletes the copies from both directories.

[VBScript]
Sub ManipFiles
  Dim fso, f1, f2, s
  Set fso = CreateObject(“Scripting.FileSystemObject”)
  Set f1 = fso.CreateTextFile(“c:\testfile.txt”, True)
  Response.Write “Writing file <br>”
  ‘ Write a line.
  f1.Write (“This is a test.”)
  ‘ Close the file to writing.
  f1.Close
  Response.Write “Moving file to c:\tmp <br>”
  ‘ Get a handle to the file in root of C:\.
  Set f2 = fso.GetFile(“c:\testfile.txt”)
  ‘ Move the file to \tmp directory.
  f2.Move (“c:\tmp\testfile.txt”)
  Response.Write “Copying file to c:\temp <br>”
  ‘ Copy the file to \temp.
  f2.Copy (“c:\temp\testfile.txt”)
  Response.Write “Deleting files <br>”
  ‘ Get handles to files’ current location.
  Set f2 = fso.GetFile(“c:\tmp\testfile.txt”)
  Set f3 = fso.GetFile(“c:\temp\testfile.txt”)
  ‘ Delete the files.
  f2.Delete
  f3.Delete
  Response.Write “All done!”
End Sub