Private Function GetFilename(FullPath As String, Optional WantExtention As Boolean = True) As String Dim pos As Integer GetFilename = FullPath If Not WantExtention Then \' Klip extention fra. pos = InStrRev(GetFilename, \".\") \' Find det sidste \".\" i strengen If pos > 1 Then GetFilename = Left(FullPath, pos - 1) End If GetFilename = Mid(GetFilename, InStrRev(GetFilename, \"\\\") + 1) End Function
winkill: jeg for en fejl i dem, fejlen er at \"InStrRev\" skal udskiftes med \"InStr\" ellers virker det ikke hos mig, og Debug.Print\" ved jeg ikke hvordan man skal bruge den men hvis jeg skriver det sådan:
Hmmm, jeg bruger InStrRev i alle mine scripts og programmer (stort set). Den gør det samme som InStr, bort set fra at den starter fra sidste tegn. Jeg bruger VB 6.0 og til scripts, den Windows Scripting Host som ligger i Windows 2000.
InStrRev er ikke en fejl, men det er muligt at den først er blevet implementeret i VB 6.0
Nå ja, og debug.print, de gange jeg har siddet og udviklet i Visual Basic miliøet har jeg lagt mærke til det lille debug vindue i bunden. Det kan aktiveres med Ctrl+G.
Hvis du skriver Debug.Print \"Hello world\" i dit program så får du \"Hello World\" frem i debug vinduet.
Nej, det virker ikke fint på den måde, for InStr starter fra begyndelsen, InStrRev starter fra slutningen... Crap VB 5.0
Nå, men Prøv lige igen, denne gang, tilføj den her funktions dimmer. (Hvad VB ikke har (havde) det laver man selv ;-)
Function InStrRev(StringCheck As String, StringMatch As String) As Long Dim pos As Long pos = Len(StringCheck) While pos > 0 If Mid(StringCheck, pos, Len(StringMatch)) = StringMatch Then InStrRev = pos Exit Function End If pos = pos - 1 Wend End Function
Function InStrRev(StringCheck As String, StringMatch As String) As Long Dim pos As Long pos = Len(StringCheck) While pos > 0 If Mid(StringCheck, pos, Len(StringMatch)) = StringMatch Then InStrRev = pos Exit Function End If pos = pos - 1 Wend End Function
Private Sub Command1_Click() Dim ThePath ThePath = \"c:\\windows\\notepad.exe\" MsgBox Mid(ThePath, InStrRev(ThePath, \"\\\") + 1) End Sub
Så for jeg fejl i linje: \"MsgBox Mid(ThePath, InStrRev(ThePath, \"\\\") + 1)\"
---------------------------- Compile error: ByRef argument type mismatch ---------------------------- \"ThePath\"
Det er fordi du har dim\'et ThePath som variable, det skal helst være en string.
Du kan bruge en af følgende.
Hvis det er ligemeget hvilken type ThePath er i dit program så brug:
Function InStrRev(StringCheck As String, StringMatch As String) As Long Dim pos As Long pos = Len(StringCheck) While pos > 0 If Mid(StringCheck, pos, Len(StringMatch)) = StringMatch Then InStrRev = pos Exit Function End If pos = pos - 1 Wend End Function
Private Sub Command1_Click() Dim ThePath As String ThePath = \"c:\\windows\\notepad.exe\" MsgBox Mid(ThePath, InStrRev(ThePath, \"\\\") + 1) End Sub
Hvis ThePath SKAL værre en variable i dit program så brug:
Function InStrRev(StringCheck As String, StringMatch As String) As Long Dim pos As Long pos = Len(StringCheck) While pos > 0 If Mid(StringCheck, pos, Len(StringMatch)) = StringMatch Then InStrRev = pos Exit Function End If pos = pos - 1 Wend End Function
Private Sub Command1_Click() Dim ThePath ThePath = \"c:\\windows\\notepad.exe\" MsgBox Mid(CStr(ThePath), InStrRev(ThePath, \"\\\") + 1) End Sub
Function InStrRev(StringCheck As String, StringMatch As String) As Long Dim pos As Long pos = Len(StringCheck) While pos > 0 If Mid(StringCheck, pos, Len(StringMatch)) = StringMatch Then InStrRev = pos Exit Function End If pos = pos - 1 Wend End Function
Private Sub Command1_Click() Dim ThePath As String ThePath = \"c:\\windows\\notepad.exe\" MsgBox Mid(ThePath, InStrRev(ThePath, \"\\\") + 1) End Sub
Ja, i VB < v6.0 vil jeg give dig fuldstændigt ret nolle.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.