By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,815 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,815 IT Pros & Developers. It's quick & easy.

Renaming and moving files.

P: 1
Hello,

I am trying to add the string "XYZ" to each file name and then move my files from folderA to folderB.
This is the code that i have so far.

**********************************
Expand|Select|Wrap|Line Numbers
  1. Private Const varSourceAddress As String = "\\serverA\drivename\FolderA\"
  2. Dim diaddr As New DirectoryInfo(varSourceAddress)
  3. Dim fiaddr As FileInfo() = diaddr.GetFiles()
  4.  
  5. While diaddr.GetFiles.Length > 0
  6. For Each scannedFile As FileInfo In fiaddr
  7. If scannedFile.Name.Contains("Thumbs") = False Then
  8. If scannedFile.Exists Then
  9. strOldName = scannedFile.Name
  10. strNewName = strOldName.Insert(14, "XYZ")
  11. scannedFile.CopyTo("\\serverA\drivename\FolderB\" & strNewName, True)
  12. scannedFile.Delete()
  13.  
  14. strOldName = ""
  15. strNewName = ""
  16.  
  17. End If
  18. End If
  19. Next
  20. End While 
  21.  
************************************************
I am using this code in a windows service. When this code executes for the first time, everything happens the way it's suppose to be. It does add XYZ to every file's name and the files get moved to folderB also. However, upon the second run, it sends an error message:

System.IO.FileNotFoundException: Could not find file '\\serverA\drivename\folderA\20130327082003.pdf'.

I can see that the file 20130327082003.pdf has already been moved to folderB with the new name of 20130327082003XYZ.pdf and that is all fine, but the service keeps checking for the same file name in folderA. I have tried rename, move commands also, but instead of checking for the new files (if any) in folderA the system keeps checking for the file that has already been moved to folderB. I am using .Delete method to delete the file (or any reference) once it has been copied to folderB, but that doesn't seem to make any difference.

Once the service will be running, eventually it's suppose to move every new file generated in folderA (after adding XYZ to its name) to folderB.

Any help will be appreciated.
Thank you.
Mar 28 '13 #1
Share this Question
Share on Google+
1 Reply


Rabbit
Expert Mod 10K+
P: 12,383
It's probably because you never refresh your fiaddr variable.
Mar 29 '13 #2

Post your reply

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