使用 FSO.BuildPath 從資料夾路徑和檔名構建完整路徑

如果你接受資料夾路徑的使用者輸入,則可能需要在構建檔案路徑之前檢查尾部反斜槓(\)。FSO.BuildPath 方法使這更簡單:

  Const sourceFilePath As String = "C:\Temp"  '<-- Without trailing backslash
  Const targetFilePath As String = "C:\Temp\" '<-- With trailing backslash

  Const fileName As String = "Results.txt"
  
  Dim FSO As FileSystemObject
  Set FSO = New FileSystemObject
  
  Debug.Print FSO.BuildPath(sourceFilePath, fileName)
  Debug.Print FSO.BuildPath(targetFilePath, fileName)

輸出:

C:\Temp\Results.txt
C:\Temp\Results.txt