Renaming and moving files.

Blofeld

New member
Joined
Mar 28, 2013
Messages
1
Programming Experience
5-10
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.

**********************************
Private Const varSourceAddress As String = "\\serverA\drivename\FolderA\"
Dim diaddr As New DirectoryInfo(varSourceAddress)
Dim fiaddr As FileInfo() = diaddr.GetFiles()

While diaddr.GetFiles.Length > 0
For Each scannedFile As FileInfo In fiaddr
If scannedFile.Name.Contains("Thumbs") = False Then
If scannedFile.Exists Then
strOldName = scannedFile3.Name
strNewName = strOldName.Insert(14, "XYZ")
scannedFile.CopyTo("\\serverA\drivename\FolderB\" & strNewName, True)
scannedFile.Delete()

strOldName = ""
strNewName = ""

End If
End If
Next
End While

************************************************
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, when the service runs for the second time, 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. Even if there is no file in folderA, still i get this message.

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

From what I can see your code looks fine, however you would only get this error if the list of files that was stored in your variable, fiaddr, never changed. So the question I would ask is, where in your code have you used these statement:-

VB.NET:
Private Const varSourceAddress As String = "\\serverA\drivename\FolderA\"
Dim diaddr As New DirectoryInfo(varSourceAddress)
Dim fiaddr As FileInfo() = diaddr.GetFiles()

If these have been declared at the class level then that's your answer since you need to recreate your new file list in your variable, fiaddr, using the GetFiles Method.

Hope that helps.

Cheers,

Ian
 
Back
Top