How would I strip a filename (in a string) of its extention.
eg
filestr="Textfile.txt"
I want filestr to = "Textfile"
Thanks :)
Garrett
Printable View
How would I strip a filename (in a string) of its extention.
eg
filestr="Textfile.txt"
I want filestr to = "Textfile"
Thanks :)
Garrett
x = instr(filestr,".")
filestr = left(filestr,x-1)
dim tmp() as string
tmp = SPlit(filestr,".")
filestr = tmp(0)
dim intPosition as integer
dim newString as string
intPosition = inStr(1, textfile.txt, ".")
NewString = left(textfile.txt, intPosition - 1)
NewString should end up = to txtfile
Or..
Code:Private Sub Command1_Click()
x = InStrRev(Text1, ".")
z = Mid(Text1, 1, x - 1)
Print z
End Sub
for x = 1 to len(filestr)
i = mid(filestr,x,1)
if i = "." then exit for
tmp = tmp & i
next x
filestr = tmp
:)
gotta cover all possibilities right?
Or, one more option.
Code:Private Sub Command1_Click()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
MsgBox fs.GetBaseName("c:/extension.txt")
End Sub
what if there are multiple "." in a file....? :)
should be......Quote:
for x = 0 to len(filestr)
i = mid(filestr,x,1)
if i = "." then exit for
tmp = tmp & i
next x
filestr = tmp
Code:For x = len(filestr) to 0 step -1
i = mid(filestr,x,1)
if i = "." then exit for
tmp = tmp & i
Next x
filestr = tmp
:P
You code print "txt", he wants the other way around.
if there are multiple "."'s....
jbart's code would be best....
just change it to take the variable
good thinking jbartCode:Private Sub Command1_Click()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
MsgBox fs.GetBaseName(filestr)
End Sub
neat piece of code, i learned something new :P
btw, how come vb doesn't autocomplete those properties
for me? I'm referencing the microsoft scripting runtime.
Is there anything else i need to reference?
It's because your using "CreateObject", if you use the syntax, you will get the IntelliSense.
Code:Dim fso as FileSystemObject
Set fso = New FileSystemObject
fso.GetBaseName
when you use creatobject...VB doesnt know what the Object will be until it is run...so it does not recognize the object name.
yep, Late Binding
ty
Thanks Lethal. I have been blindly using the code as it appears in the MSDN examples for the FileSystemObject.
With your way I get to see all the options.
Hey no prob, we both learned something new :D
Just sharing more code of how to get the filename and not the extension.
Code:Private Sub Command1_Click()
Dim sFile As String
Dim vArray As Variant
Dim uArray As Integer
sFile = "C:\Textfile.txt"
vArray = Split(sFile, ".")
uArray = LBound(vArray)
MsgBox vArray(uArray)
End Sub
If you wish to get the extension and not the filename...
Code:Private Sub Command1_Click()
Dim sFile As String
Dim vArray As Variant
Dim uArray As Integer
sFile = "C:\Textfile.txt"
vArray = Split(sFile, ".")
uArray = UBound(vArray)
MsgBox Chr$(46) & vArray(uArray)
End Sub