Extract file name from path and maybe folder

I am trying to create a file backup to a usb drive using filecopy. The mainpath is where i need help. The file could be in a sub folder under app.path. The app.path looks like this:
mainPath =C:\Program Files\MyApp\Jo Blow 147 Elm Ave.bid or could be Something else like:
mainPath =C:\Program Files\MyApp\June Bids\Jo Blow 147 Elm Ave.bid in this case i want to copy the June Bids folder and the file name. How can i do this ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gyanendra SinghArchitectCommented:
please find sample code

Private Function ExtractFileName(FullPath As String) As String  
Dim iPos As Integer  
ExtractFileName = FullPath  
iPos = InStrRev(FullPath, "\")  
If iPos > 0 Then  
ExtractFileName = Mid$(FullPath, iPos + 1)  
End If  
End Function  

Open in new window

Using API...
Private Declare Sub PathStripPathW Lib "shlwapi.dll" (ByVal pszPath As Long)
Public Function StripPath(ByVal szPath As String) As String
    Call PathStripPathW(StrPtr(szPath))
    StripPath = Left$(szPath, InStr(1, szPath, vbNullChar) - 1)
End Function

Open in new window

isnoend2001Author Commented:
Thanks egl1044
That works for giving just the file name, but need to determine if the file is in a sub folder and copy the folder name first. The file could be in a folder under App.path like: App.path & "June bids" and then the file name or:
App.path & "July bids" and then the file name or whatever. This is getting a little more involved than i thought. I guess what i need to do is find if there is a sub folder then make that folder on the flash then copy the file. How to determine if file is in a subfolder and get the folder name ?
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Remove the filename and keep the path using API
Private Declare Function PathRemoveFileSpecW Lib "shlwapi.dll" (ByVal pszPath As Long) As Boolean
Public Function RemoveFileSpec(ByVal szPath As String) As String
    ' * Removes the trailing file name and backslash from a path
    Call PathRemoveFileSpecW(StrPtr(szPath))
    RemoveFileSpec = Left$(szPath, InStr(1, szPath, vbNullChar) - 1)
End Function

Open in new window

isnoend2001Author Commented:
That does remove the file however how do i determine if the file is in a sub folder and get the folders name? It could be:
C:\Program Files\Myapp\Jo Blow 147 Elm Ave.bid  or
C:\Program Files\Myapp\July Bids\Jo Blow 147 Elm Ave.bid  or even
make a folder on flash drive
Then Mkdir FlashDriveLetter\Bidfilebackups\July Bids
or even
C:\Program Files\Myapp\July Bids\Comp Shingles\Jo Blow 147 Elm Ave.bid
Then make 2 folders on flash drive
Then Mkdir FlashDriveLetter\Bidfilebackups\July Bids
Then Mkdir FlashDriveLetter\Bidfilebackups\July Bids\Comp Shingles

Well to go off topic with your initial question. I think that what you may be doing this the hard way. How about just copy the entire folder and subfolders.

Basically straight forward Folder to Folder copy keeping the same structure.

Option Explicit
   hWnd        As Long
   wFunc       As Long
   pFrom       As String
   pTo         As String
   fFlags      As Integer
   fAborted    As Boolean
   hNameMaps   As Long
   sProgress   As String
 End Type
Private Declare Function SHFileOperationA Lib "shell32.dll" ( _
    ByRef lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Sub RtlZeroMemory Lib "kernel32.dll" ( _
    ByVal Destination As Long, _
    ByVal Length As Long)
Private Const FO_COPY                   As Long = &H2
Private Const FOF_NOCONFIRMATION        As Long = &H10
Private Const FOF_MULTIDESTFILES        As Long = &H1
Private Const FOF_SILENT                As Long = &H4
Public Sub CopyFolder(ByVal szFrom As String, ByVal szTo As String)
    ' * prepare struct
    RtlZeroMemory VarPtr(shf), Len(shf)
    ' * prepare struct with copy information
    With shf
        .wFunc = FO_COPY
        .pFrom = szFrom
        .pTo = szTo
    End With
    ' * now copy
    Call SHFileOperationA(shf)
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
isnoend2001Author Commented:
I have a routine that copies the whole structure, but there may be 100's of small files, just want to copy 1 file silently and fast. Guess i will ask another question more precise to what i need, thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.