473,386 Members | 1,752 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,386 software developers and data experts.

FileInfo.CopyTo Problem

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).strPathAndFilenameOfAdClipOnClien t
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
md******@scs-techresources.com
954-452-1047

Nov 21 '05 #1
10 5852
Try this instead:

http://www.planet-source-code.com/vb...=556&lngWId=10

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #2
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
Nov 21 '05 #3
Crouchie,
Thanks for the post, but I just don't understand why my code leaves a folder with all the files there, they just are empty. Any idea why what I did does not give me what I want--simply to copy one file to another folder and over write the existing file if the Source is newer.
Thanks,
Michael
"Crouchie1998" <cr**********@spamcop.net> wrote in message news:#u**************@TK2MSFTNGP14.phx.gbl...
Try this instead:

http://www.planet-source-code.com/vb...=556&lngWId=10

Crouchie1998
BA (HONS) MCP MCSE
Nov 21 '05 #4
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
"Cor Ligthert" <no************@planet.nl> wrote in message news:##*************@TK2MSFTNGP15.phx.gbl...
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
Nov 21 '05 #5
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
Nov 21 '05 #6
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
"Cor Ligthert" <no************@planet.nl> wrote in message
news:#9*************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #7
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.

Nov 21 '05 #8
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
Nov 21 '05 #9
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.

Nov 21 '05 #10
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.
Hi Cor,
.
.
. I copied the original files back to
where they were supposed to be and it all works great.
Thanks for your help.
Michael


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

:-)

Cor
Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stefan Turalski \(stic\) | last post by:
Hi, I used to use CopyTo() method from FileInfo class, does anyone know if there is a way to bind this with progressbar, or with some sort of paintBox - to show progress of moving/coping file ?...
1
by: Mortimer Schnurd | last post by:
Has anyone had any luck getting this CopyTo method to work? I can iterate through a MatchCollection and move each Match.Value to the System.Array without a problem. I just can't figure out why...
3
by: | last post by:
I have a collectiond derived from NameObjectCollectionBase. FxCop is complaining that I need to implement a strongly typed CopyTo(MyObjectType, int) How do I do this? I can't seem to find any...
3
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code in C#: In global--> public struct point { int x; string y; }
2
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
6
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come...
1
by: schmidtfalko | last post by:
Hi, i use the CopyTo - Function to copy large files into a net drive. Now it would be mad, if I could indicate the remaining time or also the status (in %) to the copying action. Have...
0
by: dolphinearth | last post by:
Hi. I have a strange problem around DirectoryInfo and FileInfo of C# (c- sharp). I have a File Watcher which automattically imports files from a network drive to a directory on the local...
2
by: =?Utf-8?B?Sm9uIEphY29icw==?= | last post by:
In VB I use FileInfo.CopyTo and get an error when the destination file already exists. What do I use to copy to a destination and write over the existing file? Thanks, Jon
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.