How to write a function to combine the folder path and file name?

I would like to pass the full path of the text file to one of the functions. I place my script and text file in the same place using the command below, I found the path to the folder where my script is

p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)

p appeared as C:\test

My file name xyz.txt

I want to pass the argument of the function as C:\test\xyz.txt

how can i combine path and file name

I tried under the code

path =  p & "xyz.txt"

can anyone help me how to join the path and file name.

+4
source share
2 answers

You can use string concatenation to build a path. However, the correct way to do this is to use the FileSystemObject method BuildPath(), because it will be correct with backslashes under all circumstances.

Set FSO = CreateObject("Scripting.FileSystemObject")

scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
textFilePath = FSO.BuildPath(scriptPath, "xyz.txt")

MsgBox textFilePath
+7

:

Option Explicit
Msgbox GetFilePath("xyz.txt")
'******************************************************
Function GetFilePath(FileName)
Dim fso,scriptPath
Set fso = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
GetFilePath = FSO.BuildPath(scriptPath,FileName)
End Function
'******************************************************
+2

All Articles