Solved

Extract file name from path and maybe folder

Posted on 2008-10-27
7
528 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now