Copy Directory / Folder to same location

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Karl Raams
    New Member
    • Jul 2011
    • 8

    Copy Directory / Folder to same location

    Hi,

    I've been working on some code to rename files and have being doing so by copying them to the same location under a different name and then removing the original. When I do this for files I use the code ...

    Code:
    Dim file As New System.IO.FileInfo(lstFiles.SelectedItem.ToString)
    Dim strNewFile As String
    Dim strRename As String
    Dim strMask As String = ""
    Dim intCurrentNum As Integer
    Dim cntNumber As Integer
    intCurrentNum += 1
    For cntNumber = 1 To numMask.Text
      If strMask <> "" Then
        strMask = strMask & "0"
      Else
        strMask = "0"
      End If
    Next
    strRename = txtRename.Text & Format(intCurrentNum, strMask)
    strNewFile = file.Directory.ToString & "\" & strRename & file.Extension.ToString
    file.CopyTo(strNewFile)
    file.Delete()
    ... to place a file in the same location as it was originally. So if the file is in C:\MyFolder\MyF ile.txt the new one goes into C:\MyFolder\MyN ewFile.txt. My Question is how do I achieve this same effect with folders / directories. For example I want Folder C:\MyMainFolder \MyFolder to create C:\MyMainFolder \MyNewFolder.

    My current code that I'm trying to get to do this looks like:

    Code:
    Dim folder As New System.IO.DirectoryInfo(lstFiles.SelectedItem.ToString)
    Dim strNewFile As String
    Dim strRename As String
    Dim strMask As String = ""
    Dim intCurrentNum As Integer
    Dim cntNumber As Integer
    intCurrentNum += 1
    For cntNumber = 1 To numMask.Text
    If strMask <> "" Then
        strMask = strMask & "0"
      Else
        strMask = "0"
      End If
    Next
    strRename = txtRename.Text & Format(intCurrentNum, strMask)
    strNewFile = folder.FullName & "\" & strRename
    MessageBox.Show(strNewFile)
    folder.MoveTo(strNewFile)
    folder.Delete()
    This will obviously not work as it will try to copy the folder into itself which is not allowed.

    If anyone has any suggestions on how to do this they would be greatly appreciated. I'm pretty new to VB.Net coding so you might have to spell things out for me a bit.

    Thanks,

    Karl
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Why are you taking this approach to renaming something instead of just renaming it directly?

    Comment

    • Karl Raams
      New Member
      • Jul 2011
      • 8

      #3
      The reason I did it this way is because when I tried some of the rename codes that I found off of searching Google I couldn't get them to work. Being new to VB.Net I wasn't even sure if I had the right code for VB.NET.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        You should just use the rename functionality.
        Code:
        My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
        RenameDirectory to rename folders.

        Comment

        • Karl Raams
          New Member
          • Jul 2011
          • 8

          #5
          Thanks Rabbit. I used this code and everything seems to be working fine.

          Comment

          Working...