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!

Extracting a file path from a string

Status
Not open for further replies.

emozley

Technical User
Jan 14, 2003
769
GB
Hi,

I've got a a whole series of files listed in a table eg:

\\bwbsrv01\users\Intranets\bwbnet\docs\root\General\Newsletters\BWB Newsletter 15.06.06.pdf

What is the best way of identifying the file path from that string. Basically I want to say anything to the left of the final slash regardless of how many folders deep it is.

Thanks very much

Ed
 
Try this:

dim mystring, filepath, ind

mystring = "\\bwbsrv01\users\Intranets\bwbnet\docs\root\General\Newsletters\BWB Newsletter 15.06.06.pdf"
ind = instrrev(mystring,"\")
filepath = Left(mystring, length(mystring)-ind)


didnt check the code...need some tweaking may be...

-DNG

 
I'll tweak DotNetGnat's example a little:


Code:
dim mystring, filepath, ind

mystring = "\\bwbsrv01\users\Intranets\bwbnet\docs\root\General\Newsletters\BWB Newsletter 15.06.06.pdf"
ind = instrrev(mystring,"\")
filepath = Left(mystring, ind)

[monkey][snake] <.
 
You might want some additional processing to take care of error situations.

Examples:
Waht if the database field is null?
What if the last character is a slash?
What if there is a folder name but no file name?
 
Hi,

Thanks for this - unfortunately the more possible errors I came across the more complicated the code got. I did a bit more research and found a very useful property called ParentFolder which has done the trick.

Here's some code I have written if you are using the Zoom indexer (from Wrensoft) and want to create desc files automatically from a database.

Thanks again,

Ed

Set DB = createobject("ADODB.Connection")
Set TBL = CreateObject("ADODB.RecordSet")

DB.Mode = adModeReadWrite
DB.Open "driver={Microsoft Access Driver (*.mdb)};dbq=mydb.mdb"

TBL.Open "SELECT DocsMaster.Title, DocsMaster.AuthorID, DocsMaster.Notes, DocsMaster.FilePath FROM DocsMaster", DB

Set objFSO = CreateObject("Scripting.FileSystemObject")
counter=0
Do While Not TBL.EOF

FilePath=TBL("FilePath")

If objFSO.FileExists(FilePath) Then

Set FilePath = objFSO.GetFile(FilePath)
DestinationFolder=FilePath.ParentFolder
DescFile=FilePath.Name & ".desc"

strFile=""
strFile=strFile & "<title>" & TBL("Title") & "</title>" & vbCRLF
strFile=strFile & "<meta name=" & CHR(034) & "author" & CHR(034) & " content=" & CHR(034) & TBL("AuthorID") & CHR(034) & ">" & vbCRLF
strFile=strFile & "<meta name=" & CHR(034) & "description" & CHR(034) & " content=" & CHR(034) & TBL("Notes") & CHR(034) & ">" & vbCRLF

Set oFiletxt = objFSO.CreateTextFile(DestinationFolder & "\" & DescFile, True)

oFiletxt.WriteLine(strFile)

oFiletxt.Close
counter=counter+1
End If

TBL.MoveNext

Loop

TBL.Close

Set objFSO=Nothing
Set DB=Nothing
Set DB=Nothing

wscript.echo counter & " .desc files complete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top