How to Carry Out VBScript File Management

Using VBScript to Create, Copy, Delete and Rename Files

© Mark Alexander Bain

Aug 15, 2009
Using VBScript to Create, Copy, Delete and Rename , Mark Alexander Bain
VBScript is a powerful computer programming language. A VBS script file can be used to create, copy and delete files. And by combining those command it can rename a file

In any VBScript project the programmer will, at some point, have to carry out operations such as:

  • VBScript delete file
  • VBScript copy file
  • VBScript rename file

However, it may surprise a new programmer that a computer programming language as powerful as VBScript does not have these command built into it. That's because it makes use of the Windows File System Object. This does have these commands built into it.

Of course, before learning how to carry out a VBScript delete file, copy file or rename file, it is worth considering to carry out a VBScript create file operation.

The VBScript Create File Method

Before doing anything else in a VBScript file, the programmer should always add the line:

Option Explicit

This ensures that all variables have to be defined before they can be used and, in this example, the variables are to be used in a subroutine (so that the code can be reused). The first of these variables in the subroutine is used to create the file system object:

' The VBScript Create File Method
Sub createFile (fileName)
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

And the second variable is used to create the file itself:

Dim file: Set file = fso.CreateTextFile(fileName, True)

Once the file has been created then the script can write to it:

file.WriteLine("Hello World")

And, at the end of the subroutine, the script closes the file and unloads the file system object:

file.Close
Set fso = nothing
End Sub

This can now be run by using:

Dim f1: f1 = "C:\testfile.txt"
createFile f1

Once the file has been created then it can be copied.

The VBScript Copy File Method

Like the VBScript create file method, the VBScript copy file method should be encapsulated into a procedure (again, so that it can be reused). This time it will be a function that reports the success or failure of the VBScript copy file operation:

' The VBScript Copy File Method
Function copyFile (fileNameFrom, fileNameTo)
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim file

It's worth noting that (assuming that all file permissions are correct and there's enough disk space) the VBScript file create method will always work. However, the VBScript copy file method will only work if the originating file exists:

if fso.fileExists (fileNameFrom) Then
Set file = fso.GetFile(fileNameFrom)
file.Copy (fileNameTo)
copyFile = True
Else
Wscript.Echo fileNameFrom & " does not exist"
copyFile = False
End If
Set fso = nothing
End Function

This can be run by using the code:

Dim f1: f1 = "C:\testfile.txt"
Dim f2: f2 = "C:\testfile2.txt"
createFile f1
copyFile f1, f2

The next step is to delete any unwanted files.

The VBScript Delete File Method

The VBScript delete file method follows the same general format of the preceding two, and again a check is needed to ensure that the file exists before it can be deleted:

' The VBSCript Delete File Method
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim file
if fso.fileExists (fileName) Then
Set file = fso.GetFile(fileName)
file.Delete
End If
Set fso = nothing
End Sub

This be run by using:

Dim f1: f1 = "C:\testfile.txt"
deleteFile f1

The final operation is the VBScript file rename.

A VBScript Rename File Subroutine

There is no rename functionality in VBScript or the file system object that it uses. However, it is easy to achieve this by combining the VBScript file copy method and the VBScript file delete method:

' A VBScript Rename File Subroutine
Sub renameFile (fileNameFrom, fileNameTo)
If copyFile (fileNameFrom, fileNameTo) Then
deleteFile fileNameFrom
End If
End Sub

Which can then be run by using:

Dim f1: f1 = "C:\testfile.txt"
Dim f2: f2 = "C:\testfile2.txt"
createFile f1
renameFile f1, f2

In this way the VBScript application developer can create a set of file management subroutines and functions that can be used in any VBScript program that they go on to write.


The copyright of the article How to Carry Out VBScript File Management in Windows Programming is owned by Mark Alexander Bain. Permission to republish How to Carry Out VBScript File Management in print or online must be granted by the author in writing.


Using VBScript to Create, Copy, Delete and Rename , Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo