Solved

Extract file name from path and maybe folder

Posted on 2008-10-27
7
527 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now