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