473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 ...

Expand|Select|Wrap|Line Numbers
  1. Dim file As New System.IO.FileInfo(lstFiles.SelectedItem.ToString)
  2. Dim strNewFile As String
  3. Dim strRename As String
  4. Dim strMask As String = ""
  5. Dim intCurrentNum As Integer
  6. Dim cntNumber As Integer
  7. intCurrentNum += 1
  8. For cntNumber = 1 To numMask.Text
  9.   If strMask <> "" Then
  10.     strMask = strMask & "0"
  11.   Else
  12.     strMask = "0"
  13.   End If
  14. Next
  15. strRename = txtRename.Text & Format(intCurrentNum, strMask)
  16. strNewFile = file.Directory.ToString & "\" & strRename & file.Extension.ToString
  17. file.CopyTo(strNewFile)
  18. file.Delete()
  19.  
... to place a file in the same location as it was originally. So if the file is in C:\MyFolder\MyFile.txt the new one goes into C:\MyFolder\MyNewFile.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:

Expand|Select|Wrap|Line Numbers
  1. Dim folder As New System.IO.DirectoryInfo(lstFiles.SelectedItem.ToString)
  2. Dim strNewFile As String
  3. Dim strRename As String
  4. Dim strMask As String = ""
  5. Dim intCurrentNum As Integer
  6. Dim cntNumber As Integer
  7. intCurrentNum += 1
  8. For cntNumber = 1 To numMask.Text
  9. If strMask <> "" Then
  10.     strMask = strMask & "0"
  11.   Else
  12.     strMask = "0"
  13.   End If
  14. Next
  15. strRename = txtRename.Text & Format(intCurrentNum, strMask)
  16. strNewFile = folder.FullName & "\" & strRename
  17. MessageBox.Show(strNewFile)
  18. folder.MoveTo(strNewFile)
  19. folder.Delete()
  20.  
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
Jul 25 '11 #1

✓ answered by Rabbit

You should just use the rename functionality.
Expand|Select|Wrap|Line Numbers
  1. My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
RenameDirectory to rename folders.

4 1920
Rabbit
12,516 Expert Mod 8TB
Why are you taking this approach to renaming something instead of just renaming it directly?
Jul 25 '11 #2
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.
Jul 26 '11 #3
Rabbit
12,516 Expert Mod 8TB
You should just use the rename functionality.
Expand|Select|Wrap|Line Numbers
  1. My.Computer.FileSystem.RenameFile("C:\Test.txt", "SecondTest.txt")
RenameDirectory to rename folders.
Jul 26 '11 #4
Thanks Rabbit. I used this code and everything seems to be working fine.
Jul 27 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in...
1
by: Josh | last post by:
I am using IO to copy files, and would like to use it to copy folders. Is there a simple way to copy a folder and all the contents within it? I am using VB.NET.
4
by: John Blair | last post by:
Hi, Can anyone tell me how to make the Copy Project option work from within Vb.net - it fails on my PC with an error saying the new copy directory is marked as executable and i am not allowed to...
3
by: TN | last post by:
Am I missing something here? I am checking the radio button "Download prerequisites from the same location as my application" so that I can include the .Net 2.0 framework with my application...
6
by: singhal.prateek | last post by:
Hi, How can we make two objects point to the same location? For e.g. I have an object obj1 of a class and I want to have another object obj2 to point to the member variables of obj1. Something...
0
by: dotnetnoob | last post by:
i got "could not complete operation on some files and directroies. see the data property of the expection for more details" while trying to copy a folder from the server to my local PC. the...
12
by: samoore33 | last post by:
I found a real nice article on how to copy a directory on MSDN. The only problem is that I can not figure out how to get the namespace Microsoft.VisualBasic.MyServices. I wanted to know if anyone...
13
by: Puja | last post by:
hi all, how can I copy directory c:\test to a directory c:\backup with all files, folders and subfolders that are contained within test. please advice! thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.