Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Renaming and moving files in windows service

Status
Not open for further replies.

nondrinker

Programmer
Jan 23, 2002
53
US
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, 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.





 
I've never created a service - however assuming that it is running in a continuous loop, I tried this as a simple test. Basically I just clear out fiaddr at the end of each iteration so that it is empty prior to being filled - with the For loop simulating the continuous monitoring of the folder.

Code:
Public Class Form1

	Private diaddr As IO.DirectoryInfo
	Private fiaddr() As IO.FileInfo

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		For a As Integer = 0 To 2

			diaddr = New IO.DirectoryInfo("C:\")
			fiaddr = diaddr.GetFiles

			MessageBox.Show(a.ToString & ": " & fiaddr.Length.ToString)
			Array.Resize(fiaddr, 0)
			MessageBox.Show(a.ToString & ": " & fiaddr.Length.ToString)

		Next

	End Sub
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top