Other FSO Methods and Properties
The base FSO object offers a few other useful methods and properties that you may need from time to time.
The first is the BuildPath function. It accepts components of a file or folder path and appends them together. Normally, you could do that with the simple & concatenation operator, but BuildPath actually worries about getting backslashes in the right place. So, consider this example:
Dim sFolder, sFile
sFolder = "C:\Windows"
sFile = "MyFile.exe"
Dim oFSO
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
MsgBox sFolder & sFile
MsgBox oFSO.BuildPath(sFolder,sFile)
The first message box displays "C:\WindowsMyFile.exe", which isn't right-it is missing the backslash in the middle. The second message box, which uses BuildPath, displays the correct "C:\Windows\MyFile.exe" because the BuildPath function figured out that a backslash was necessary.
While working with paths, you may also have a need to get the absolute or base path name, and the FSO's GetAbsolutePathName and GetBaseName methods will do it for you. Here's an example.
Dim oFSO, sPath1, sPath2
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
sPath1 = "C:\Windows\System32\Scrrun.dll"
sPath2 = "..\My Documents\Files"
MsgBox oFSO.GetAbsolutePathName(sPath1)
MsgBox oFSO.GetAbsolutePathName(sPath2)
MsgBox oFSO.GetBaseName(sPath1)
MsgBox oFSO.GetBaseName(sPath2)
The result of this is four message boxes.
"C:\Windows\System32\Scrrun.dll":
There's no difference between the input and output, because the input in this case is already a complete, unambiguous path.
"C:\Documents and Settings\Administrator\My Documents\Files":
This is an example output you might see. The difference is that the path has been resolved into a complete, final path starting at the root of the drive.
"Scrrun":
This is the base name of the last component in the input, without any file extension.
"Files":
Again, this is the base name of the last component, although in this case it's a folder instead of a file.
Finally, there's GetTempName. If you need to create a temporary file or folder, especially within the system's temporary files folder, it's important that you use a filename that other applications won't already be using. GetTempName simply makes up a filename that is unique, allowing you to create your temp file with confidence.
|