Results 1 to 5 of 5

Thread: Trouble Renaming File with Textbox input

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Cool Trouble Renaming File with Textbox input

    I have been struggling a bit trying to get this to work. I am trying to rename the file but can't seem to get it to work. The file creates but just doesn't seem to rename with the text in the text box. Any ideas to point me in the right direction would be great. Below is the code.

    Code to generate File Code:
    1. Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    2.  
    3.         If File.Exists("c:\Sample.txt") Then
    4.             'shows message if testFile exist
    5.             MsgBox("File Already Exist ")
    6.         Else
    7.             'create the file testFile.txt
    8.             File.Create("c:\Sample.txt")
    9.             MsgBox("File Has Been Created ")
    10.         End If
    11.     End Sub

    Code to Rename File Code:
    1. Public Sub RenameFile()
    2.  
    3.         Dim mOldFilePath As String = "C:\Sample.txt"
    4.         'Dim mExtension As String
    5.         Dim mNewFileName As String
    6.         mNewFileName = txtName.Text
    7.  
    8.  
    9.         My.Computer.FileSystem.RenameFile(mOldFilePath, mNewFileName)

    Thanks in Advance

    RCA20

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble Renaming File with Textbox input

    Why create the file and then rename it? Why not just create it with the correct name in the first place?

    As for the issue, File.Create doesn't just create a file. It also opens it and returns a FileStream with which you can write to the file. You won't be able to rename, delete, etc, that file until that FileStream is closed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Trouble Renaming File with Textbox input

    this allows you to change the filename and the extension

    vb Code:
    1. Public Sub RenameFile()
    2.     Dim mOldFilePath As String = "C:\Sample.txt"
    3.     Dim mOldFileName As String = IO.Path.GetFileNameWithoutExtension(mOldFilePath)
    4.     Dim mOldExtension As String = IO.Path.GetExtension(mOldFilePath)
    5.     Dim mNewFileName As String = IO.Path.GetFileNameWithoutExtension(txtName.Text)
    6.     Dim mNewExtension As String = If(IO.Path.GetExtension(txtName.Text) <> "", IO.Path.GetExtension(txtName.Text), mOldExtension)
    7.    My.Computer.FileSystem.RenameFile(mOldFilePath, mOldFilePath.replace(mOldFileName & mOldExtension, mNewFileName & mNewExtension)))
    8. end sub

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Trouble Renaming File with Textbox input

    Paul thanks for your help. After playing with the code I still haven't been able to get it too work.

    jmcilhinney you may be right but I just couldn't figure out any other way to do it. Being new to this I thought I had to create the file first then write to that file. Problem was that the user gets too save the file name at runtime.

    Example
    Name:  MiscSave.bmp
Views: 175
Size:  263.5 KB

    The goal was to allow the user to save the text that was generate in a richtextbox from a different form to the specific folder and name that file. Maybe I am going about it all the wrong way and considering I am new too this I wouldn't doubt it.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Trouble Renaming File with Textbox input

    Just store the file path selected by the user in a String variable. When it comes time write your text to the file you simply call File.WriteAllText or whatever and specify the file path using that variable.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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