Link to home
Start Free TrialLog in
Avatar of isnoend2001
isnoend2001Flag for United States of America

asked on

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 ?
Avatar of Gyanendra Singh
Gyanendra Singh
Flag of India image

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

Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

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

Avatar of isnoend2001

ASKER

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 ?
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

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

ASKER CERTIFIED SOLUTION
Avatar of nffvrxqgrcfqvvc
nffvrxqgrcfqvvc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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