2017-09-19 3 views
1

C'est ce que j'ai:VBS - Retirez une partie de chaîne après la dernière «/»

Dim pathBefore 
pathBefore = "product/subproduct/item/item" 

C'est ce que je dois obtenir de pathBefore:

Dim pathAfter 
pathAfter = "product/subproduct/item" 

Ce que je peux faire pour atteindre ce? J'ai essayé avec RegEx mais ce n'est pas une solution acceptable.

Répondre

0

Trouver la dernière / puis lu jusqu'à ce point - 1

pathAfter = left$(pathBefore, instrrev(pathBefore, "/") - 1) 
+1

Le $ est une antiquité invalide. –

1

Essayez ce code:

Dim pathBefore, pathAfter, temp, i 
pathBefore = "product/subproduct/item/item" 
temp = Split(pathBefore,"/") 
For i=0 To UBound(temp)-1 
    pathAfter = pathAfter & temp(i) & "/" 
Next 
pathAfter = Left(pathAfter,Len(pathAfter)-1) 
MsgBox pathAfter 

Sortie:

enter image description here

+0

Ne pas, il existe de meilleures (plus efficace/plus facile pour les yeux) des alternatives. –

+0

@ Ekkehard.Horner Sera heureux d'apprendre d'eux. C'était la première façon qui me venait à l'esprit. – Gurman

2

@Gurman:

>> s = "product/subproduct/item/item" 
>> a = Split(s, "/") 
>> ReDim Preserve a(UBound(a) - 1) 
>> WScript.Echo Join(a, "/") 
>> WScript.Echo goFS.GetParentFolderName(s) 
>> WScript.Echo Left(s, InstrRev(s, "/") - 1) 
>> 
product/subproduct/item 
product/subproduct/item 
product/subproduct/item 
+0

Ils sont sûrement meilleurs. Merci pour cela :) – Gurman