Results 1 to 19 of 19

Thread: Get external file path

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Question Get external file path

    I came out with this code with the intention of renaming my files from explorer context menu:

    Code:
    Private Sub Form_Load()
    Dim strFile As String
    strFile = Dir("*.txt")
    Name strFile As "MyFile.txt"
    End
    End Sub
    But this is not the right way to do it. it takes a bit of delay.

    What would it be the best way of getting the path of the selected file in a textbox? then it will be easier to rename it having the file path.
    The problem is that my file is not always in the same folder, that's way I added no path in my code.

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Get external file path

    use a FileListBox. Set the path of it to the folder you want.
    Then select the file you want to rename, preface it with the FileListBox path and do your renaming.

    Example...add a textbox and a filelistbox to a form

    add this code

    Code:
    Private Sub File1_Click()
        Name File1.Path & "\" & File1.FileName As App.Path & "\" & Text1.Text
    End Sub
    
    
    Private Sub Form_Load()
        File1.Path = App.Path
    End Sub
    run it, type in a 'new name' in the textbox, and then click on the file in your filelistbox you want to rename

    (YOU determine what path the file is in by setting the filelistbox (file1) to that path)
    Last edited by SamOscarBrown; Feb 5th, 2021 at 02:20 PM.
    Sam I am (as well as Confused at times).

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Hello Sam, I really appreciate your answer.
    But my Form doesn't have a given path to the file to be rename it.
    That's because I access my file from anywhere on my desktop and from any folder.

    For example: if I go to any folder on my desktop, documents, etc.
    Then I right-click and click on my context menu: Preview, the file gets rename it: Preview.
    in this case 'Preview.txt'. my file the one that renames the file is always in: C:\MyFile.exe.

    This all takes place without showing my app and then the file gets renamed.
    But what I was after is for a way of adding a textbox to my form,
    and then when a selected file gets ready to be rename it.. to have my app to collect the path
    of that selected file to be rename it in a textbox.
    But I have tried everything and I cannot find a way to do it.
    Attached Images Attached Images  

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Get external file path

    okay...step by step please

    1-you want a user to right click SOMEWHERE on a file in SOME folder on the computer.
    (is #1 correct?)

    2-when that happens (if correctly stated above), you want 'your program' to have a textbox on it to do what? Bring that filename into it?
    (Is that correct?)

    3-assuming 1 and 2 are correct, you want the user to CHANGE what is in the textbox to a NEW NAME and then 'save' it (that is, rename that file, keeping it in the same directory)?
    (is THAT correct?)

    EDIT: If all is correct, do this...add a commondialog (under components) to your form along with a textbox and two commandbuttons.

    add this code:

    Code:
    Option Explicit 'NOTE...if you already have Option Explicit on your from, do not add it again.
    
    Dim myFileAndPathName As String
    Dim myPath As String
    Private Sub Command1_Click()
        CommonDialog1.ShowOpen
        myFileAndPathName = CommonDialog1.FileName
        Dim i As Integer
        i = InStrRev(myFileAndPathName, "\")
        myPath = Mid(myFileAndPathName, 1, i)
        Text1.Text = Mid(myFileAndPathName, i + 1)
    End Sub
    
    
    Private Sub Command2_Click()
        Name myFileAndPathName As myPath & Text1.Text
    End Sub
    run it, use command1 to browse ANYWHERE on the computer/network to locate the file you want to rename
    then, CHANGE the name in the textbox
    then, use command2 to rename it. (I assume that the file you selected is not already open in some other program/or not running, as in an .exe., otherwise you will have an error)
    Last edited by SamOscarBrown; Feb 5th, 2021 at 05:24 PM.
    Sam I am (as well as Confused at times).

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Thanks Sam, let me explain...

    1.) That user will be me, just me and yes, the purpose of my file is to go any where in my computer and use the right-click menu context that I created it in the registry to select a file and rename it with an assigned name already in my app. (In this case: Preview.txt).

    2.) I've been using it this way for a while now. then I recently came out with the idea of adding a textbox to the project to grab the path of the file before it gets rename it.

    3.) And no, the idea is just to grags the file path and pass it to the textbox thats all.

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Get external file path

    Is the filename being passed to your application as a parameter? If so, then in Form_Load you should be able to add something like this:

    Code:
    txtFile.Text = Command$

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    This is all the code in the app:
    Code:
    Private Sub Form_Load()
    Dim strFile As String
    strFile = Dir("*.txt")
    Name strFile As "MyFile.txt" 'In this case: Preview.txt
    End
    End Sub
    Then in the registry I have this parameter:
    Code:
    HKEY_CLASSES_ROOT\*\shell\Rename\shell\Preview\command: C:\MyApp.exe "%1"
    My project reanme any text file without typing anything to it.
    The name is automatically added it by my app and then the
    file gets renamed instanlly without using a path of the file.
    Nor CommonDialog controls, or without browsing or anything else.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Hey Sam, thank you so much, I did not see this coming, thanks a lot!

  9. #9
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Get external file path

    I'm not following the logic behind your code at all. The registry setting that you posted indicates that you are passing the filename that was right-clicked as a parameter to your program (that's what the "%1" accomplishes), but then in your code you are not using that at all and instead doing a Dir for *.txt.

    If I'm interpreting that correctly, then if you have a folder with filenames of "A.txt", "B.txt", and "C.txt", and you right-clicked on "C.txt" and chose Rename->Preview, I would imagine that your program would instead rename "A.txt" to "Preview.txt" (or to "MyFile.txt", since the code you posted has that hardcoded), since "A.txt" would be what is first found by the Dir command.

    Good luck.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Hello Option,
    I know my questions and my projects are always are hard for others to understand, but it does works for me. and the story behind this code it is bery useful to me. for example not long ago I created another project which create files for me. since every day I create so many times the same file 'Readme.txt' in this case. I decided to create the same as the project above, but with the differents of this other project to create a file 'Readme.txt' with content already in the file. saving me the time of creating a new text document file and then give it a name and then finally typing the content in it. and so far I like it a lot. but thank you for your opinion.

  11. #11
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Get external file path

    Good luck then.

  12. #12
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Get external file path

    I attached a sample project that does what you asked.
    The code:

    Code:
    Private Sub Form_Load()
        txtPath.Text = Command$
        txtPath.Locked = True
    End Sub
    
    Private Sub cmdOK_Click()
        Dim Pos As Long
        
        Pos = InStrRev(txtPath.Text, "\")
        On Error GoTo ErrHandler
        Name txtPath.Text As Left$(txtPath.Text, Pos) & "Preview.txt"
        Unload Me
        Exit Sub
        
    ErrHandler:
        MsgBox "Error renaming file", vbCritical
        Unload Me
    End Sub
    
    Private Sub cmdCancel_Click()
        Unload Me
    End Sub
    BTW, the registry key for this to work should be:

    Code:
    HKEY_CLASSES_ROOT\*\shell\Custom renaming\command
    with the Default value set to
    Code:
    path\MyApp.exe %1
    %1 without quotes.

    'Custom renaming' cannot be just 'Rename', at least it does not work here (in Windows 10 in English). It must be some custom caption.
    Attached Files Attached Files

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Quote Originally Posted by OptionBase1 View Post
    Good luck then.
    Hey Option, I'm so sorry, I should read messages better next time. actually it was you who posted the answer to my question. I wanted to apologize and say thank you for your great answer. Thanks again.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2013
    Posts
    330

    Re: Get external file path

    Quote Originally Posted by Eduardo- View Post
    I attached a sample project that does what you asked.
    The code:

    Code:
    Private Sub Form_Load()
        txtPath.Text = Command$
        txtPath.Locked = True
    End Sub
    
    Private Sub cmdOK_Click()
        Dim Pos As Long
        
        Pos = InStrRev(txtPath.Text, "\")
        On Error GoTo ErrHandler
        Name txtPath.Text As Left$(txtPath.Text, Pos) & "Preview.txt"
        Unload Me
        Exit Sub
        
    ErrHandler:
        MsgBox "Error renaming file", vbCritical
        Unload Me
    End Sub
    
    Private Sub cmdCancel_Click()
        Unload Me
    End Sub
    BTW, the registry key for this to work should be:

    Code:
    HKEY_CLASSES_ROOT\*\shell\Custom renaming\command
    with the Default value set to
    Code:
    path\MyApp.exe %1
    %1 without quotes.

    'Custom renaming' cannot be just 'Rename', at least it does not work here (in Windows 10 in English). It must be some custom caption.
    Hello Eduardo,

    I wanted to thank you for your code, thanks again.
    But at the end all I wanted was to get the path of the file.
    Thanks

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Get external file path

    I am not sure if you got everything you need already or not...but, if you use the SHIFT-RightMouseClick you will see a "Copy As Path". Selecting that will put the entire path and file name in the Clipboard. Then paste into your program. No need for Registry changes.
    Sam I am (as well as Confused at times).

  16. #16
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Get external file path

    Er, have you even looked at the value returned by a Command$() call?

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Get external file path

    me, or op?
    Sam I am (as well as Confused at times).

  18. #18
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,995

    Re: Get external file path

    Quote Originally Posted by dilettante View Post
    Er, have you even looked at the value returned by a Command$() call?
    Not sure to whom you are asking.

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Get external file path

    I was answering the question.

    If he hacked the registry to run his program passing the file path as a command line parameter then it might be logical to look there to find it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width