FileInfo.CopyTo Problem

M

Michael Murphy

Hi,
I have a Windows VB.Net app in which I need to keep files in one folder in sync with files in another folder. I have pasted the code below. Can anyone tell me why I end up with a folder with all the file names correct, but the length of each file is zero.
Thanks for your help.
Michael

Public Function SyncFiles() As Integer
Dim CopyToPath As String
Dim CopyFromPath As String
Dim CopyToPathFileInfo As FileInfo
Dim CopyFromPathFileInfo As FileInfo

Try
CopyAllFiles = False
For i As Integer = 0 To intNumberOfSlots - 1
CopyFromPath = AdSlotRecords(i).strPathAndFilenameOfAdClip
CopyFromPathFileInfo = New FileInfo(CopyFromPath)
CopyToPath = AdSlotRecords(i).strPathAndFilenameOfAdClipOnClient
CopyToPathFileInfo = New FileInfo(CopyToPath)
If CopyFromPathFileInfo.Exists Then ' Make sure source exists
If CopyToPathFileInfo.Exists Then ' If target exists check for latest version
If CopyFromPathFileInfo.LastWriteTime > CopyToPathFileInfo.LastWriteTime Then
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
Return False
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function

--
Michael D. Murphy
Senior Software Architect
SCS-TechResources, Inc.
1400 NW 70 Way
Suite HO1
Plantation, FL 33313-5330
(e-mail address removed)
954-452-1047
 
C

Cor Ligthert

Michael,

When you show us code, than it should basicly work in my opinion.
To do that set first option strict to on in top of your program.

You start now with showing us a function which should return an integer and returns a boolean.

As well have you some functions incorporated which is impossible to understand what it does when you don't inlcude them.

The function start by instance with
For i As Integer = 0 To intNumberOfSlots - 1

Where intNumbersOfSlots - 1 can be anything

Maybe can you sent us some code, that is not at the start already a puzzle.

Cor
 
M

Michael Murphy

Hi Cor,
Thanks for the posting tips. Let me rewrite my current post so it is easier to see recognize a problem.
Thanks for your time.
Michael

Public Function SyncFiles() As Boolean
Dim CopyToPath As String
Dim CopyFromPath As String
Dim CopyToPathFileInfo As FileInfo
Dim CopyFromPathFileInfo As FileInfo

Try
CopyAllFiles = False
For i As Integer = 0 To 8 ' 8 files in the source folder
' SourceFileNames is an array of strings for the path and filenames of each of the files to be copied from the source folder
' DestFilenames is an array of strings for the path and filenames of each of the files to be copied from the source folder
CopyFromPath = SourceFileNames (i)
CopyFromPathFileInfo = New FileInfo(CopyFromPath)
CopyToPath = DestFilenames(i)
CopyToPathFileInfo = New FileInfo(CopyToPath)
If CopyFromPathFileInfo.Exists Then ' Make sure source file exists
If CopyToPathFileInfo.Exists Then ' If target exists check for latest version
If CopyFromPathFileInfo.LastWriteTime > CopyToPathFileInfo.LastWriteTime Then
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
Return False
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
Michael,

When you show us code, than it should basicly work in my opinion.
To do that set first option strict to on in top of your program.

You start now with showing us a function which should return an integer and returns a boolean.

As well have you some functions incorporated which is impossible to understand what it does when you don't inlcude them.

The function start by instance with
For i As Integer = 0 To intNumberOfSlots - 1

Where intNumbersOfSlots - 1 can be anything

Maybe can you sent us some code, that is not at the start already a puzzle.

Cor
 
C

Cor Ligthert

Michael,

I made your sample testable. However, it did what it should do in my
opinion.

You see some changes, I have brought the declarations inside the loop. In my
opinion gives this a nicer memory management. (Everytime are the values
deleted from the stack and are the objects direct given free to the GC to
destroy). However that has nothing to do with the error you told.

Just try this sample, it is complete, however you have to made some
directorys and one testfile.

(It is basicly your sample, without the global settings and the not needed
boolean I did not change anything, although that I prefer just because it is
easier to write the io.file.copy method).

\\\
Public Function SyncFiles() As Boolean
Dim SourceFileNames() As String = New String()
{"C:\test1\WhatEver.txt"}
Dim DestFileNames() As String = New String()
{"C:\test2\WhatEver2.txt"}
Try
For i As Integer = 0 To SourceFileNames.Length - 1
' SourceFileNames is an array of strings for the path and
filenames of each of the files to be copied from the source folder
' DestFilenames is an array of strings for the path and
filenames of each of the files to be copied from the source folder
Dim CopyFromPath As String = SourceFileNames(i)
Dim CopyFromPathFileInfo As New IO.FileInfo(CopyFromPath)
Dim CopyToPath As String = DestFileNames(i)
Dim CopyToPathFileInfo As New IO.FileInfo(CopyToPath)
If CopyFromPathFileInfo.Exists Then ' Make sure source file
exists
If CopyToPathFileInfo.Exists Then ' If target exists
check for latest version
If CopyFromPathFileInfo.LastWriteTime >
CopyToPathFileInfo.LastWriteTime Then
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
Return False
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
///

I hope this helps,

Cor
 
M

Michael Murphy

Hi Cor,
I tried your sample using a small text file, and it worked, so I replaced
the textfile name with a the path and name of one of the files that I want
to copy. And I was left with the same filesize of 0. Anyway, I used a Create
method above the CopyTo, and that must have truncated the file. So from that
point on it was doing it correctly, the difference now being it was copying
a 0 length file instead of the orignal. I copied the original files back to
where they were supposed to be and it all works great.
Thanks for your help.
Michael
 
P

Peter Huang [MSFT]

Hi

Based on my test, your code is OK.
Here is my test code which will copy the files in certain directory to
another one.

Also I think you may set a debug break point and run the code one by one to
see what is the problem.

Dim CopyAllFiles As Boolean
Public Function SyncFiles() As Integer
Dim CopyToPath As String
Dim CopyFromPath As String
Dim CopyToPathFileInfo As FileInfo
Dim CopyFromPathFileInfo As FileInfo
Try
CopyAllFiles = False
Dim dir As New DirectoryInfo("C:\Test")
For Each f As FileInfo In dir.GetFiles("*.*")
CopyFromPathFileInfo = f
CopyToPath = "C:\Temp\" & f.Name
CopyToPathFileInfo = New FileInfo(CopyToPath)
If CopyFromPathFileInfo.Exists Then ' Make sure source
exists
If CopyToPathFileInfo.Exists Then ' If target exists
check for latest version
If CopyFromPathFileInfo.LastWriteTime >
CopyToPathFileInfo.LastWriteTime Then
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
CopyFromPathFileInfo.CopyTo(CopyToPath, True)
End If
Else
Return False
End If
Next
Return True
Catch ex As Exception
Return False
End Try
End Function

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

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Crouchie1998

I don't understand why your code is so long. It can be done in under half
that

Crouchie1998
BA (HONS) MCP MCSE
Official Microsoft Beta Tester
 
P

Peter Huang [MSFT]

Hi

Yes and thanks for your feedback.
Because I wants to duplicate Michael's problem, so I try to make change to
Michael's code as small as possible.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Cor Ligthert

Hi Peter,
Yes and thanks for your feedback.
Because I wants to duplicate Michael's problem, so I try to make change to
Michael's code as small as possible.

But why do you sent that too a message that ends with this.

It makes the thread not more readable on Google, however I think you missed
that and no problem.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top