Results 1 to 9 of 9

Thread: Renaming a file

  1. #1

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335

    Cool

    Solving my previous question (and I'm very thankful for that), came up in my head a renaming feature!
    I have he same FileListBox named fleNotes and a commandbutton cmdRename.
    How can I do the following?:
    1. The user clicks on cmdRename
    2. The app pops up an InputBox
    3. The user types in the new name (let's put in a variable)
    4. The fleNotes.ListIndex file is renamed to the new name.
    5. Refresh fleNotes

    I know how to do 1,2,3,5. No. 4 is my problem!
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  2. #2
    Guest
    Add this code to your button
    Code:
    Private Sub cmdRename_Click()
        retval = InputBox("Enter a new name")
        If retval <> "" Then Name fleNotes As retval
        fleNotes.Refresh
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335

    Unhappy

    'File Not Found', Megatron...
    Any suggestions?
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Megatron 's code works for me. I know this seems obvious, but are you sure you have an item selected in the list box before you try to rename it???

    P.S. What is the code in your footer supposed to mean? And it looks like it wouldn't run anyway...you should say "Next Num" instead of "Next i"


    [Edited by seaweed on 11-07-2000 at 06:44 PM]
    ~seaweed

  5. #5

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335

    Red face

    Yes I've selected a file and used the code but it shows me that damn error...
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Another seemingly obvious thing (but I have to mention it since these things are usually the ones that kill us) is that if you are using a DriveListBox and/or a DirListBox (allowing the user to select other folders or drives than the one your application resides in) then you will have to pass this information along with the file name to the Name function.

    Example:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim NewName As String
        
        NewName = InputBox("Enter a new name")
        
        If NewName <> "" Then Name Dir1 & "\" & File1 As Dir1 & "\" & NewName
        
        File1.Refresh
    End Sub
    
    Private Sub Dir1_Change()
        File1.Path = Dir1.Path
    End Sub
    
    Private Sub Drive1_Change()
        Dir1.Path = Drive1.Drive
    End Sub
    ~seaweed

  7. #7
    Frenzied Member Jop's Avatar
    Join Date
    Mar 2000
    Location
    Amsterdam, the Netherlands
    Posts
    1,986
    Yeah, it's important to pass the Path to the file in both parameters.

    Code:
    File1 = "C:\MyFolder\Myfile"
    File2 = "C:\MyFolder\Myrenamedfile"
    Name File1 As File2
    Jop - validweb.nl

    Alcohol doesn't solve any problems, but then again, neither does milk.

  8. #8
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    You can also use the Name function to move a file

    If you specify a different path in the NewName string, you will end up moving the file to the new folder, along with a new name.
    Code:
    File1 = "C:\MyFolder\Myfile"
    File2 = "C:\MyOtherFolder\MyRenamedFile"
    Name File1 As File2
    This will move MyFile from MyFolder to MyOtherFolder and rename it to MyRenamedFile.

    Pretty cool, huh?
    ~seaweed

  9. #9

    Thread Starter
    Hyperactive Member Asaf_99's Avatar
    Join Date
    Jul 2000
    Location
    Israel
    Posts
    335
    Originally posted by seaweed
    Another seemingly obvious thing (but I have to mention it since these things are usually the ones that kill us) is that if you are using a DriveListBox and/or a DirListBox (allowing the user to select other folders or drives than the one your application resides in) then you will have to pass this information along with the file name to the Name function.

    Example:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        Dim NewName As String
        
        NewName = InputBox("Enter a new name")
        
        If NewName <> "" Then Name Dir1 & "\" & File1 As Dir1 & "\" & NewName
        
        File1.Refresh
    End Sub
    
    Private Sub Dir1_Change()
        File1.Path = Dir1.Path
    End Sub
    
    Private Sub Drive1_Change()
        Dir1.Path = Drive1.Drive
    End Sub
    Hey thanks y'all! This code worked fine!!!
    Asaf Sagi

    ICQ: 61917199
    E-Mail: [email protected]

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