Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vbscript

I would like to pass the full path of a text file to one of the function. i am placing the my script, and text file at same location by using the below command i found the folder path where my script is

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

p came as C:\test

my file name is xyz.txt

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

how can i combine path and file name

i tried below code

path =  p & "xyz.txt"

can any one help me how can join the path and file name.

like image 353
surendra Avatar asked Mar 04 '16 12:03

surendra


People also ask

How do I join file path and FileName in Python?

path. join() method in Python join one or more path components intelligently. This method concatenates various path components with exactly one directory separator ('/') following each non-empty part except the last path component.

How do I follow a path to a file?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

What is path combine?

Combines two strings into a path. Combine(String, String, String) Combines three strings into a path. Combine(String, String, String, String) Combines four strings into a path.

How do you combine paths in Java?

You can combine paths by using the resolve method.


2 Answers

You can use string concatenation to build a path. The correct way to do it, however, is to use the FileSystemObject's BuildPath() method, because this will do the right thing with the backslashes under all circumstances.

Set FSO = CreateObject("Scripting.FileSystemObject")

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

MsgBox textFilePath
like image 83
Tomalak Avatar answered Nov 04 '22 07:11

Tomalak


Try like this code :

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
'******************************************************
like image 45
Hackoo Avatar answered Nov 04 '22 05:11

Hackoo