?
Solved

Extract file name from path and maybe folder

Posted on 2008-10-27
7
Medium Priority
?
536 Views
Last Modified: 2012-06-27
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 ?
0
Comment
Question by:isnoend2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22819348
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

0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 22820829
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

0
 

Author Comment

by:isnoend2001
ID: 22823380
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 ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 22824466
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

0
 

Author Comment

by:isnoend2001
ID: 22824780
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

0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 2000 total points
ID: 22824984
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
 
Private Type SHFILEOPSTRUCT
   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)
 
    Dim shf As SHFILEOPSTRUCT
    ' * prepare struct
    RtlZeroMemory VarPtr(shf), Len(shf)
    ' * prepare struct with copy information
    With shf
        .wFunc = FO_COPY
        .pFrom = szFrom
        .pTo = szTo
        .fFlags = FOF_SILENT Or FOF_NOCONFIRMATION Or FOF_MULTIDESTFILES
    End With
    ' * now copy
    Call SHFileOperationA(shf)
    
End Sub

Open in new window

0
 

Author Closing Comment

by:isnoend2001
ID: 31510632
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question