Results 1 to 7 of 7

Thread: [RESOLVED] rename files in file listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    Resolved [RESOLVED] rename files in file listbox

    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.
    Last edited by myvbopps; Oct 16th, 2006 at 05:21 PM. Reason: misspelling

  2. #2
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: rename files in file listbox

    Welcome to the forums!

    What do you mean with, place into a file listbox?

    Try something like this:
    VB Code:
    1. Option Explicit
    2.     Dim i As Integer
    3.  
    4. Private Sub Command1_Click()
    5.     Dim sPath As String: sPath = File1.Path
    6.  
    7.         If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    8.  
    9.             For i = 0 To File1.ListCount - 1
    10.                 Name (sPath & File1.List(i)) As (sPath & Text1.Text & String(i + 1, "x"))
    11.             Next i
    12.  
    13.         File1.Refresh
    14. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    Re: rename files in file listbox

    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.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: rename files in file listbox

    Welcome to the forums.

    Renaming a file is fairly straightforward.
    VB Code:
    1. Name "c:\temp.txt" As "c:\temp2.txt"
    Once renamed, you can "move" it using FileCopy
    VB Code:
    1. FileCopy "c:\temp2.txt", "c:\differentfolder\temp2.txt"
    2. Kill "c:\temp2.txt"
    These can be incorporated to use your listbox entries.

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    3

    Re: rename files in file listbox

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

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: rename files in file listbox

    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:
    1. Option Explicit
    2.     Dim i As Integer
    3.  
    4. Private Sub Command1_Click()
    5.     Dim counter As Integer: counter = -1
    6.     Dim sPath As String: sPath = File1.Path
    7.  
    8.     List1.Clear
    9.  
    10.     For i = 0 To File1.ListCount - 1
    11.         If File1.Selected(i) = True Then
    12.             List1.AddItem File1.List(i)
    13.         End If
    14.     Next i
    15.  
    16.     If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    17.  
    18.     For i = 0 To List1.ListCount - 1
    19.         counter = counter + 1
    20.         Name sPath & List1.List(i) As sPath & Text1.Text & Format(counter, "00#")
    21.     Next i
    22.  
    23.     File1.Refresh
    24.     List1.Clear
    25.  
    26.     MsgBox "Done!"
    27. End Sub

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: rename files in file listbox

    You might want to place the counter in the base name rather than appending it to the extension

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