|
-
Feb 7th, 2001, 12:53 PM
#1
Thread Starter
Addicted Member
How would I strip a filename (in a string) of its extention.
eg
filestr="Textfile.txt"
I want filestr to = "Textfile"
Thanks 
Garrett
-
Feb 7th, 2001, 12:58 PM
#2
couple ways
x = instr(filestr,".")
filestr = left(filestr,x-1)
dim tmp() as string
tmp = SPlit(filestr,".")
filestr = tmp(0)
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2001, 12:59 PM
#3
Addicted Member
This should work
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
-
Feb 7th, 2001, 01:05 PM
#4
PowerPoster
Or..
Code:
Private Sub Command1_Click()
x = InStrRev(Text1, ".")
z = Mid(Text1, 1, x - 1)
Print z
End Sub
-
Feb 7th, 2001, 01:11 PM
#5
ok...
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?
Last edited by Static; Feb 7th, 2001 at 01:37 PM.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2001, 01:22 PM
#6
Fanatic Member
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
-
Feb 7th, 2001, 01:23 PM
#7
Hyperactive Member
what if there are multiple "." in a file....? 
for x = 0 to len(filestr)
i = mid(filestr,x,1)
if i = "." then exit for
tmp = tmp & i
next x
filestr = tmp
should be......
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
Bababooey
Tatatoothy
Mamamonkey
-
Feb 7th, 2001, 01:28 PM
#8
PowerPoster
You code print "txt", he wants the other way around.
-
Feb 7th, 2001, 01:40 PM
#9
good point...
if there are multiple "."'s....
jbart's code would be best....
just change it to take the variable
Code:
Private Sub Command1_Click()
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
MsgBox fs.GetBaseName(filestr)
End Sub
good thinking jbart
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2001, 02:09 PM
#10
Hyperactive Member
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?
Bababooey
Tatatoothy
Mamamonkey
-
Feb 7th, 2001, 02:11 PM
#11
PowerPoster
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
-
Feb 7th, 2001, 02:13 PM
#12
when you use creatobject...VB doesnt know what the Object will be until it is run...so it does not recognize the object name.
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Feb 7th, 2001, 02:15 PM
#13
PowerPoster
-
Feb 7th, 2001, 02:15 PM
#14
Hyperactive Member
Bababooey
Tatatoothy
Mamamonkey
-
Feb 7th, 2001, 02:44 PM
#15
Fanatic Member
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.
-
Feb 7th, 2001, 02:48 PM
#16
PowerPoster
Hey no prob, we both learned something new
-
Feb 7th, 2001, 02:56 PM
#17
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|