Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there equivalent in VB .NET to the VFP'S ADDBS()?

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
568
US
Colleagues,

The subject line says it.
For those not familiar with VFP:
ADDBS(strPath) checks presence of the backslash at the end of the given path, e.g.

Code:
strPath = "C\MyPath"
lsPath = ADDBS(strPath) 'Returns "C:\MyPath\"
I've no problem to "roll-er-own" function in VB, but need to make sure I'm not re-inventing the wheel...

TIA!

Regards,

Ilya
 
You may find some information in here, who knows....?


---- Andy

There is a great need for a sarcasm font.
 
Or my version (somewhat more powerful than simply adding a backslash):

Code:
[blue]    Private Function ADDBS(strPath As String) As String
        ADDBS = IO.Path.GetFullPath(Trim(IO.Path.Combine(strPath, " ")))
    End Function[/blue]
 
Thank you, Andy, but I cannot use it: Company's policies! :-(

So, I did "roll-er-own", here's what I have so far:

Code:
'===================================================================================================================================
Public Function AddBackSlash(ByVal tsPath As String) As String
'===================================================================================================================================
' Purpose       : Ensures the given path string has backslash at the end.
' Description   : Checks if the passed parameter's data type match those of the Function's argument; if not – throws error message 
'                 and returns unchanged parameter.
'                 Checks if it's an empty string, if it is - returns it As-Is.
'                 Checks if it's only a file name, if it is - returns it As-Is.
'                 Checks the given parameter-string in case it's full path to a file, cuts off the file name if it is.
'                 Checks if the given parameter-string has backslash at the end, adds it if it has not.
' Parameters    : Path as String - mandatory
' Returns       : Path with the backslash at the end, as String.
' Side effects  : None.
' Notes         : 1. Generic, applies with .NET Framework ver. 1.1, .NET Core 1.0, .NET Standard 1.0 and higher.
'                 2. Verbose on errors, silent otherwise.
' Author        : Ilya
' Revisions     : 2020-03-09 by Ilya – started 1st draft.
'===================================================================================================================================
Dim lsPath As String = "", lsRet As String = ""

' Parameter's verification
If VarType(tsPath) <> VariantType.String Then
   MsgBox("Invalid parameter passed: " & Chr(34) & "tsPath" & Chr(34) & " must be of type String", MsgBoxStyle.Critical, "Fatal Error: INVALID PARAMETER")
   Return lsRet
End If

If String.IsNullOrEmpty(tsPath) Then
   Return lsRet
End If

If Path.GetFileName(tsPath) <> "" And Path.GetExtension(tsPath) <> "" Then 'Path + File name? Strip off that latter
   lsPath = tsPath.Replace(Path.GetFileName(tsPath), "")
Else
   lsPath = tsPath
End If

If String.IsNullOrEmpty(lsPath) Then ' Only the file name was passed? Return blank string
   Return lsPath
End If

' Check for the closing backslash
If Strings.Right(lsPath, 1) <> Path.DirectorySeparatorChar Then
   lsRet = lsPath & Path.DirectorySeparatorChar
Else
   lsRet = lsPath
End If

Return lsRet
End Function
'===================================================================================================================================

Please review and advise if I've missed anything.

TIA!
 
Take a look at the String.EndsWith function.

As to your function:

' Parameter's verification
If VarType(tsPath) <> VariantType.String Then
MsgBox("Invalid parameter passed: " & Chr(34) & "tsPath" & Chr(34) & " must be of type String", MsgBoxStyle.Critical, "Fatal Error: INVALID PARAMETER")
Return lsRet
End If

This part is not necessary. Trying to pass in anything other than a string will produce a design-time error, and your program will not even compile much less run.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
>Trying to pass in anything other than a string will produce a design-time error

Not quite true - VB.NET will do type conversion - but your point remains valid, since the conversion will result in a string, so the check never fails
 
>[green] Checks if it's only a file name, if it is - returns it As-Is.[/green]

Are you sure?.

 
And one more question - why do you think you need to do this?

I ask because if you use the Path methods it is fairly simple to build correct paths without worrying about adding in backslashes or whatever


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top