473,802 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CopyToPathFileI nfo As FileInfo
Dim CopyFromPathFil eInfo As FileInfo

Try
CopyAllFiles = False
For i As Integer = 0 To intNumberOfSlot s - 1
CopyFromPath = AdSlotRecords(i ).strPathAndFil enameOfAdClip
CopyFromPathFil eInfo = New FileInfo(CopyFr omPath)
CopyToPath = AdSlotRecords(i ).strPathAndFil enameOfAdClipOn Client
CopyToPathFileI nfo = New FileInfo(CopyTo Path)
If CopyFromPathFil eInfo.Exists Then ' Make sure source exists
If CopyToPathFileI nfo.Exists Then ' If target exists check for latest version
If CopyFromPathFil eInfo.LastWrite Time > CopyToPathFileI nfo.LastWriteTi me Then
CopyFromPathFil eInfo.CopyTo(Co pyToPath, True)
End If
Else
CopyFromPathFil eInfo.CopyTo(Co pyToPath, 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.c om
954-452-1047

Nov 21 '05 #1
10 5899
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 intNumberOfSlot s - 1

Where intNumbersOfSlo ts - 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
"Crouchie19 98" <cr**********@s pamcop.net> wrote in message news:#u******** ******@TK2MSFTN GP14.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 CopyToPathFileI nfo As FileInfo
Dim CopyFromPathFil eInfo 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)
CopyFromPathFil eInfo = New FileInfo(CopyFr omPath)
CopyToPath = DestFilenames(i )
CopyToPathFileI nfo = New FileInfo(CopyTo Path)
If CopyFromPathFil eInfo.Exists Then ' Make sure source file exists
If CopyToPathFileI nfo.Exists Then ' If target exists check for latest version
If CopyFromPathFil eInfo.LastWrite Time > CopyToPathFileI nfo.LastWriteTi me Then
CopyFromPathFil eInfo.CopyTo(Co pyToPath, True)
End If
Else
CopyFromPathFil eInfo.CopyTo(Co pyToPath, 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:##******** *****@TK2MSFTNG P15.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 intNumberOfSlot s - 1

Where intNumbersOfSlo ts - 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\What Ever.txt"}
Dim DestFileNames() As String = New String()
{"C:\test2\What Ever2.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 CopyFromPathFil eInfo As New IO.FileInfo(Cop yFromPath)
Dim CopyToPath As String = DestFileNames(i )
Dim CopyToPathFileI nfo As New IO.FileInfo(Cop yToPath)
If CopyFromPathFil eInfo.Exists Then ' Make sure source file
exists
If CopyToPathFileI nfo.Exists Then ' If target exists
check for latest version
If CopyFromPathFil eInfo.LastWrite Time >
CopyToPathFileI nfo.LastWriteTi me Then
CopyFromPathFil eInfo.CopyTo(Co pyToPath, True)
End If
Else
CopyFromPathFil eInfo.CopyTo(Co pyToPath, 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******** *****@TK2MSFTNG P12.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\What Ever.txt"}
Dim DestFileNames() As String = New String()
{"C:\test2\What Ever2.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 CopyFromPathFil eInfo As New IO.FileInfo(Cop yFromPath)
Dim CopyToPath As String = DestFileNames(i )
Dim CopyToPathFileI nfo As New IO.FileInfo(Cop yToPath)
If CopyFromPathFil eInfo.Exists Then ' Make sure source file exists
If CopyToPathFileI nfo.Exists Then ' If target exists
check for latest version
If CopyFromPathFil eInfo.LastWrite Time >
CopyToPathFileI nfo.LastWriteTi me Then
CopyFromPathFil eInfo.CopyTo(Co pyToPath, True)
End If
Else
CopyFromPathFil eInfo.CopyTo(Co pyToPath, 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 CopyToPathFileI nfo As FileInfo
Dim CopyFromPathFil eInfo As FileInfo
Try
CopyAllFiles = False
Dim dir As New DirectoryInfo(" C:\Test")
For Each f As FileInfo In dir.GetFiles("* .*")
CopyFromPathFil eInfo = f
CopyToPath = "C:\Temp\" & f.Name
CopyToPathFileI nfo = New FileInfo(CopyTo Path)
If CopyFromPathFil eInfo.Exists Then ' Make sure source
exists
If CopyToPathFileI nfo.Exists Then ' If target exists
check for latest version
If CopyFromPathFil eInfo.LastWrite Time >
CopyToPathFileI nfo.LastWriteTi me Then
CopyFromPathFil eInfo.CopyTo(Co pyToPath, True)
End If
Else
CopyFromPathFil eInfo.CopyTo(Co pyToPath, 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(B yVal sender As System.Object, ByVal e As
System.EventArg s) 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

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

Similar topics

3
7579
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 ? -- best regards Stic
1
597
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 the CopyTo method will not work. Example: // Find all the Tables in an html string (page) MatchCollection mc = Regex.Matches(html, "<table.+</table"); string t = new string;
3
3618
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 implementations of CopyTo() that don't rely on Array.Copy or some other internal .NET method. Unfortunately, there's nothing I can find that will work for a NameObjectCollectionBase derived class. I can't find any implementations of CopyTo() for...
3
2539
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
3587
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
3023
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 across a problem I can't really explain: When deleting an object from an IStorage, the space it used up will not be freed, but rather marked as unused and overwritten the next time you add an object to the storage. This is obviously working as...
1
2056
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 unfortunately still nothing found... Is there a possibility?
0
2693
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 machine. Here is the code I used: DirectoryInfo dir = new DirectoryInfo(SAPImportPath); FileInfo fileList = dir.GetFiles("*.txt");
2
2128
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
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.