locked
RENAME & MOVE A FILE TO A NEW FOLDER

    Question

  • What piece of code could determine if the new path does already exist, and if not , create a same?

    For instance on my form I have two TextBoxes i.e. TxtBx1 and TxtBx2.

    TxtBx1 presents the complete path and name of an existing file like:

        C:\Users\Faraz\Documents\Office\Test1\ABC.xlsx

    TxtBx2 presents a new path and new name for the same like:

        C:\Users\Faraz\Documents\Home\Test2\XYZ.xlsx

    If there already exists a path C:\Users\Faraz\Documents\Home\Test2\ I want the Button BtnGo to simply move the file to the said folder and be renamed from ABC.xlsx to XYZ.xlsx. However, if such a new path doesn't exist at all, i.e. even there doesn't exist a folder like C:\Users\Faraz\Documents\Home how to create the same via SHELL and MD and also a subfolder of Test2\ within the same and then move the file to the newly created path and finally being renamed?

    Please do consider the said control names in this regard.

    Thanx in advance for all your help.


    Best Regards, Faraz A Qureshi

    • Edited by FARAZ A QURESHI Saturday, December 03, 2011 7:06 PM Correction of a Typo Error
    Saturday, December 03, 2011 6:55 PM

Answers

  • You're describing the My.Computer.FileSystem.MoveFile method.
    Saturday, December 03, 2011 10:10 PM
  • Hi,

        Private Sub BtnGo_Click(sender As System.Object, e As System.EventArgs) Handles BtnGo.Click
            'do also check the path of textbox2, if you want it
            If My.Computer.FileSystem.FileExists(TxtBx1.Text) AndAlso Not String.IsNullOrEmpty(TxtBx2.Text) Then
                Try
                    'there's also an overload with a third parameter (bool overwrite).
                    'see http://msdn.microsoft.com/en-us/library/fh3171xd(v=VS.90).aspx
                    My.Computer.FileSystem.MoveFile(TxtBx1.Text, TxtBx2.Text)
                Catch
                    'do whatever you want on error
                    'see the exceptions that may be thrown here:
                    'http://msdn.microsoft.com/en-us/library/fh3171xd(v=VS.90).aspx
                End Try
            End If
        End Sub

    Regards,

      Thorsten


    Tuesday, December 06, 2011 9:21 AM

All replies