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!

Trimming a string 1

Status
Not open for further replies.

jshurst

Programmer
Oct 27, 2004
1,158
0
0
US
Using VS2005. I am attempting to copy the entire contents of a directory (well just the files right now - not folders). I can get all of the file paths by using "Directory.Getfiles()" and then looping through the elements.

My problem comes in when I attempt to copy them and I need to give a file name. I would like to use the same file name just in a different location but I don't know how to trim the old path.

Example:
I want to get "AUTHORS" out of "C:\Program Files\Ares\AUTHORS" (AUTHORS is the file name). Can someone tell me how to do this, or if there is another way to do this? Thanks.
 
Dim sTest, sFileName As String
Dim iPos As Integer

sTest = "C:\Program Files\Ares\AUTHORS"

iPos = sTest.LastIndexOf("\") + 1
sFileName = sTest.Substring(iPos, sTest.Length - iPos)
 
Thanks everyone. 3DDD your way works perfectly.
 
I would recommend using the FileInfo class instead. It knows about directory separator characters, drive letters, etc. and you won't have to deal with them yourself. Your code will be more portable, too.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Can you give me an example of the code? Because it seems to me that that class returns a boolean value and I don't know what to do with it.
 
If all you need is the filename use the Path command:
Code:
System.IO.Path.GetFileName([i]myFolder\myFile[/i])
If you still want to use FileInfo:
Code:
Dim FI As FileInfo
Dim sFile as String
For Each sFile In Directory.GetFiles([i]myfolder[/i])
     FI = New FileInfo(sFile)
Next
FI = Nothing
From here you can get all sorts of information about the file from the FI variable, i.e. FileName, CreationDate, FileLength, blahblahblah.

Maybe this world is another planet’s Hell.
Aldous Huxley

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top