VBScript - Scripting.FileSystemObject

Here is a sample of creating an ascii file

1
2
3
4
5
6
7
8
9
10
11
12
dim dta

dta = "Some sample text"

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)

a.WriteLine("This is a test.")
a.WriteLine (dta)

a.Close

Here is a sample of copying a file from one directory to another.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
dim fso
set fso=createobject("Scripting.FileSystemObject")

on error resume next

fso.copyfile strRootDir & "Images\Picture" & datepart("d",date) & ".gif", strRootDir

if err=0 then
strError = now & strRootDir & "Images\Picture" & datepart("d",date) _
& ".gif copied successfully"

else

strError = now & " - Problem trying to copy " _
& strRootDir & "Images\Picture" & datepart("d",date) & ".gif to " _
& strRootDir & " - Error was ' & err & " - " & err.description

end if

set fso=nothing

on error goto 0

Here is a function that will count the number of files in a directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Function ComputeFileCount(folderspec)

Dim fso, f, f1, fc, s, fCount

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files

fCount=0

For Each f1 in fc
fCount=fCount+1
Next

ComputeFileCount=fCount

End Function

Here is a function to create a directory

1
2
3
4
5
6
7
Sub MakeDirectory(DirectoryName)

Set oFS = CreateObject("Scripting.FileSystemObject")

Set oFolder = oFS.CreateFolder(DirectoryName)

end sub

Does file exist

1
2
3
4
5
6
7
8
9
10
11
12
function FileExists( FileName)

Set oFSO = CreateObject("Scripting.FileSystemObject")

If oFSO.FileExists(Filename) Then
FileExists=True
Else
FileExists=False
End If

set oFSO = nothing
end function

Get the filesize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function GetFileSize(FileName)

Set oFSO = CreateObject("Scripting.FileSystemObject")

Dim file

set file = oFSO.GetFile(Filename)

getFilesize = file.Size

set file=nothing

set oFSO = nothing

end function