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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String manipulation 1

Status
Not open for further replies.

plarocque

MIS
Jul 2, 2003
26
0
0
CA
I'm quite new with vbscript and the simplest thing is difficult right now. If anyone can give ma a clue, here is my problem. I try to isolate a filename from a full path. ex: c:\folder1\folder2\folder3\filename.txt, I have this path in a variable, I need to isolate the filename.txt to eventualy join it to another path. Of course, at that point I don't know the length of that variable, neither the exact filename, would be too easy. Can somebody show me how? Thanks in advance.
 
You want to use the GetFileName method of the file system object:

Code:
dim fso
set fso = CreateObject( "Scripting.FileSystemObject" )

dim sFullName, sJustName, sJustPath
sFullName = "c:\folder1\folder2\folder3\filename.txt"
sJustPath = fso.GetParentFolderName( sFullName )
sJustName = fso.GetFileName( sFullName )

' sJustPath -> "c:\folder1\folder2\folder3"
' sJustName -> "filename.txt"

For manipulating file names, the methods of the file system object are the best things to use, but since you asked about string manipulation in general, the routines you want to look at are: Len, InStr, InStrRev, Left, Right, Mid, etc.

Len returns the length of a string, InStr returns the position of a character in a string, and InStrRev returns the position of a character starting from the end of the string. Left, Right and Mid are routines to extract a substring from a string. Look these functions up in the VBScript help file and you will find links to more.

To get just the filename using these functions, you would do something like this:

Code:
dim sFullName, sJustName
dim iSlash

sFullName = "c:\folder1\folder2\folder3\filename.txt"
iSlash    = InStrRev( sFullName, "\" )
sJustName = Mid( sFullName, iSlash + 1 )

-xift
 
well, a starting point with that varaible exmaple
right(Mystr,InStr(4,Mystr, "\")+1)
right function takes the count of a value from the right and the given numeric length in the parameter. In this case you give it the instr (position) of the 4 instance of the \.

but that will not help you with dynamic url's right. my suggestion is to use a match regex then
this is going to be fairly getneric but it will get you started off in the right direction
write a function to do the match first
this will be a example on a bases only of a valid path sent to the function:

Function RegExpTest(strng)
Dim regEx, Matches
Set regEx = New RegExp
regEx.Pattern = "\w*\.\w*"
' what this pattern says is
' any word (character) plus a "." plus the ext being the same to the regex as the file name
' you can do this as you shouldn't have . in the directory names
regEx.IgnoreCase = True
regEx.Global = True

Set Matches = regEx.Execute(strng)
RegExpTest = Matches(0)
' you're only going to return one value so the mathc array is element 0

End Function

'test the function
MsgBox(RegExpTest("c:\folder1\folder2\folder3\filename.txt"))




____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
xift's option is good also as I see I was typing to slow. [lol] little more over head I think though.

you can make that pattern more fool proof also if you needed. ext add and such

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
Try something like this, using the functions Len(string), InStrRev(string1, string2), and Right(string,length):
[tt]
Dim strVar, strFile
Dim lngLoc, lngStrLen

strVar = "C:\Windows\Program Files\Stuff\foo.txt"
lngStrLen = Len(strVar)
lngLoc = InStrRev(strVar,"\")
If lngLoc <> 0 Then
strFile = Right(strVar,lngStrLen - lngLoc)
End If
[/tt]
 
wonder how many variations we can come up with [smile]

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
Depends on how many different programmers reply to this thread. ;)
 
I generally use the InStrRev or FSO solution (depending on context), but just for the heck of it am going to also suggest Split:

Dim JustPath
Dim JustFile
Dim Dummy
Dim FullPath

FullPath = &quot;c:\demo\new folder\test.txt&quot;

Dummy = Split(FullPath, &quot;\&quot;)
JustFile = Dummy(UBound(Dummy))

JustPath = Left(FullPath, Len(FullPath) - Len(JustFile))
 
Thanks to all for your answers. I finally did it with a SPLIT but I like the FSO option better, I think I'll change my code. The problem I had with the FSO method before is that I just don't know all the Methods and the Properties available for an object. Does anyone know where I can find such usefull information. Once again THANK YOU very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top