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 ...
... 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:
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
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()
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()
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
Comment