How do I rename files I placed into a file listbox. The new file name will be entered in a text box. I also want the files to be sequential (myFilex, myFilexx, myFilexxx, etc) if the file exists then continue the numbering scheme.
Printable View
How do I rename files I placed into a file listbox. The new file name will be entered in a text box. I also want the files to be sequential (myFilex, myFilexx, myFilexxx, etc) if the file exists then continue the numbering scheme.
Welcome to the forums! :wave:
What do you mean with, place into a file listbox?
Try something like this:
VB Code:
Option Explicit Dim i As Integer Private Sub Command1_Click() Dim sPath As String: sPath = File1.Path If Right(sPath, 1) <> "\" Then sPath = sPath & "\" For i = 0 To File1.ListCount - 1 Name (sPath & File1.List(i)) As (sPath & Text1.Text & String(i + 1, "x")) Next i File1.Refresh End Sub
Sorry , what I ultimately want is to select files in a file list box, place them into a list box and rename and move them into a directory of my coice. I have accomplished everything except renaming and moving.
Welcome to the forums. :wave:
Renaming a file is fairly straightforward.Once renamed, you can "move" it using FileCopyVB Code:
Name "c:\temp.txt" As "c:\temp2.txt"These can be incorporated to use your listbox entries.VB Code:
FileCopy "c:\temp2.txt", "c:\differentfolder\temp2.txt" Kill "c:\temp2.txt"
I think I've totally confused matters. Here is what I am attempting:
To write a program that will rename selected files from 001 to how many files were selcted. I have list boxes that show the files and directories. I want to be able to select the files from the list box, press the command button and have the selected files renamed according a textbox text. Sorry for confusing matters!!
Ok. Put FileListBox, ListBox, TextBox and CommandButton on your form and try the folowing code (select files you want to rename and click the button):
VB Code:
Option Explicit Dim i As Integer Private Sub Command1_Click() Dim counter As Integer: counter = -1 Dim sPath As String: sPath = File1.Path List1.Clear For i = 0 To File1.ListCount - 1 If File1.Selected(i) = True Then List1.AddItem File1.List(i) End If Next i If Right(sPath, 1) <> "\" Then sPath = sPath & "\" For i = 0 To List1.ListCount - 1 counter = counter + 1 Name sPath & List1.List(i) As sPath & Text1.Text & Format(counter, "00#") Next i File1.Refresh List1.Clear MsgBox "Done!" End Sub
You might want to place the counter in the base name rather than appending it to the extension