|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FileInfo.CopyTo ProblemI 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 mdmur***@scs-techresources.com 954-452-1047 Try this instead:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=556&lngWId=10 Crouchie1998 BA (HONS) MCP MCSE 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" <crouchie1998@spamcop.net> wrote in message news:#ugYxerPFHA.2972@TK2MSFTNGP14.phx.gbl... Try this instead:http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=556&lngWId=10 Crouchie1998 BA (HONS) MCP MCSE 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 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" <notmyfirstn***@planet.nl> wrote in message news:##kaAqrPFHA.580@TK2MSFTNGP15.phx.gbl... When you show us code, than it should basicly work in my opinion.Michael, 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 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 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 Show quoteHide quote "Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message news:#9dmQasPFHA.244@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 > > 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. 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 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. Hi Peter,
> Yes and thanks for your feedback. But why do you sent that too a message that ends with this.> Because I wants to duplicate Michael's problem, so I try to make change to > Michael's code as small as possible. >> Hi Cor, It makes the thread not more readable on Google, however I think you missed >>. >>. >>. I copied the original files back to >> where they were supposed to be and it all works great. >> Thanks for your help. >> Michael that and no problem. :-) Cor
How to access var outside of class?
Creating events in VB.net read app.config File pointer to be positioned at the last occurance of a keyword How to back up an object... A simple question MDI ignoring Handles for Ctl+F6 MDIChild form controls: how to pass information between them? Unexpected behaviour of MyClass (please ignore 1st post) Schedule task in VB.NET |
|||||||||||||||||||||||