From: LondonLad on
Hi

The code is fromRandy Birch's CopyFileEx: Create a File Backup App
Since my earlier post this part of the code appears to do what I would
expect but I do not get the folder copied.
Can you help?

Private Declare Function CopyFile Lib "kernel32" _
Alias "CopyFileA" (ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, ByVal bFailIfExists As Long) _
As Long

Private Function BackupSourceFolder(ByVal hFileSource As Long, _
ByVal sSourceFolder As String, _
WFDSource As WIN32_FIND_DATA, _
ByVal sTargetFolder As String) As Long

'common local working variables
Dim sPath As String
Dim sRootSource As String
Dim sTmp As String
Dim sTargetMsg As String
Dim sSourceMsg As String
Dim diff As Long

'variables used for the source files and folders
Dim dwSourceFileSize As Long

'variables used for the target files and folders
Dim WFDTarget As WIN32_FIND_DATA
Dim hTargetFile As Long
Dim dwTargetFileSize As Long

sRootSource = QualifyPath(sSourceFolder)
sPath = sRootSource & "*.*"

'last check!
If hFileSource <> INVALID_HANDLE_VALUE Then

Do

'remove trailing nulls from the first retrieved object
sTmp = TrimNull(WFDSource.cFileName)



'if the object is not a folder.. CHANGED <> TO =
'If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <>
FILE_ATTRIBUTE_DIRECTORY Then
If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) =
FILE_ATTRIBUTE_DIRECTORY Then


'check for the corresponding file
'in the target folder by using the API
'to locate that specific file
hTargetFile = FindFirstFile(sTargetFolder & sTmp, WFDTarget)

'if the file is located in the target folder..
If hTargetFile <> INVALID_HANDLE_VALUE Then

'get the file size for the source and target files
dwSourceFileSize = FileGetFileSize(WFDSource)
dwTargetFileSize = FileGetFileSize(WFDTarget)

'compare the dates.
'If diff = 0 source and target are the same
'If diff = 1 source is newer than target
'If diff = -1 source is older than target
diff = FileCompareFileDates(WFDSource, WFDTarget)

'if the dates, attributes and file times
'are the same...
If (dwSourceFileSize = dwTargetFileSize) And _
WFDSource.dwFileAttributes = WFDTarget.dwFileAttributes
And _
diff = 0 Then

'...the files are the same, so take
'appropriate action (here, this is
'to simply list the files for info)
List1.AddItem sTmp & vbTab & _
dwSourceFileSize & vbTab & _
WFDSource.dwFileAttributes & vbTab & _
"files the same"

List2.AddItem sTmp & vbTab & _
dwTargetFileSize & vbTab & _
WFDTarget.dwFileAttributes & vbTab & _
"No"

Else

'files are not the same

If diff = 1 Then
'perform the preferred copy method ONLY if
'diff indicated that the source was newer!
Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
sTmp, False)

sTargetMsg = "Yes"
sSourceMsg = "source newer"

ElseIf diff = -1 Then
'source is older
sTargetMsg = "No"
sSourceMsg = "source older"

ElseIf diff = 0 Then
'the dates are the same but the file attributes
'are different. Since the date didn't change,
'replacing the file is a judgement call for
'the developer. Uncomment the line below if
'you want to copy this file, or alternatively,
'add a checkbox in your app the user can select
'to force an overwrite of files with similar dates.
sTargetMsg = "No"
sSourceMsg = "attr different"
Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
sTmp, False)

End If

'debug only: add the files to the
'lists with the appropriate message
List1.AddItem sTmp & vbTab & _
dwSourceFileSize & vbTab & _
WFDSource.dwFileAttributes & vbTab & _
sSourceMsg

List2.AddItem sTmp & vbTab & _
dwTargetFileSize & vbTab & _
WFDTarget.dwFileAttributes & vbTab & _
sTargetMsg

End If 'If dwSourceFileSize

'since the target file was found,
'close the handle
Call FindClose(hTargetFile)

Else:

'the target file was not found so
'copy the file to the target directory
Call CopyFile(sSourceFolder & sTmp, sTargetFolder & sTmp,
False)

'info only: add the files to the lists
List1.AddItem sTmp & vbTab & _
"target file did not exist"

List2.AddItem sTmp & vbTab & _
dwTargetFileSize & vbTab & _
WFDTarget.dwFileAttributes & vbTab & _
"Yes"

End If 'If hTargetFile
End If 'If WFDSource.dwFileAttributes

'clear the local variables
dwSourceFileSize = 0
dwTargetFileSize = 0
'added this code to show all files are selected
If sTmp <> "." And sTmp <> ".." Then Text3.Text = Text3.Text + 1

'check Source and Target and Folder Name
Debug.Print sSourceFolder & sTmp
Debug.Print sTargetFolder & sTmp

Loop While FindNextFile(hFileSource, WFDSource)

End If

End Function

With the debug.print :-
sSourceFolder = D:\My Photos\
sTargetFolder = F:\BackUp My Photos\
sTmp = Photos 2006
There are no delimiters around this information



From: Kevin Provance on
Top posted:

Wow, when one can't even make Randy's code right off his site work...what
does that tell ya? <g>

Answer: Don't mess with things you don't understand and check out VB's
"Move" statement.

Do folders with spaces in them requires quotes? Check that out first. Are
you checking for the existance of folders before moving files? *you* are
responsible for checking for and creating folders...it's something you
should be doing before *any* file operation. I would also throw in checks
for the existence of files you are about to copy. It may seem redundant,
but way stranger things have happened.

Besides the ever annoying "it don't work" statement, are there any errors?
error messages? Return values from the line of code that is failing? I'll
tell you what I tell the simplest of customers I deal with: "it don't work"
doesn't tell me a thing.


"LondonLad" <LondonLad(a)discussions.microsoft.com> wrote in message
news:C0AF8F79-8969-4FDB-ACDC-3F04F50AB5AA(a)microsoft.com...
:
: Hi
:
: The code is fromRandy Birch's CopyFileEx: Create a File Backup App
: Since my earlier post this part of the code appears to do what I would
: expect but I do not get the folder copied.
: Can you help?
:
: Private Declare Function CopyFile Lib "kernel32" _
: Alias "CopyFileA" (ByVal lpExistingFileName As String, _
: ByVal lpNewFileName As String, ByVal bFailIfExists As Long) _
: As Long
:
: Private Function BackupSourceFolder(ByVal hFileSource As Long, _
: ByVal sSourceFolder As String, _
: WFDSource As WIN32_FIND_DATA, _
: ByVal sTargetFolder As String) As Long
:
: 'common local working variables
: Dim sPath As String
: Dim sRootSource As String
: Dim sTmp As String
: Dim sTargetMsg As String
: Dim sSourceMsg As String
: Dim diff As Long
:
: 'variables used for the source files and folders
: Dim dwSourceFileSize As Long
:
: 'variables used for the target files and folders
: Dim WFDTarget As WIN32_FIND_DATA
: Dim hTargetFile As Long
: Dim dwTargetFileSize As Long
:
: sRootSource = QualifyPath(sSourceFolder)
: sPath = sRootSource & "*.*"
:
: 'last check!
: If hFileSource <> INVALID_HANDLE_VALUE Then
:
: Do
:
: 'remove trailing nulls from the first retrieved object
: sTmp = TrimNull(WFDSource.cFileName)
:
:
:
: 'if the object is not a folder.. CHANGED <> TO =
: 'If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <>
: FILE_ATTRIBUTE_DIRECTORY Then
: If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) =
: FILE_ATTRIBUTE_DIRECTORY Then
:
:
: 'check for the corresponding file
: 'in the target folder by using the API
: 'to locate that specific file
: hTargetFile = FindFirstFile(sTargetFolder & sTmp, WFDTarget)
:
: 'if the file is located in the target folder..
: If hTargetFile <> INVALID_HANDLE_VALUE Then
:
: 'get the file size for the source and target files
: dwSourceFileSize = FileGetFileSize(WFDSource)
: dwTargetFileSize = FileGetFileSize(WFDTarget)
:
: 'compare the dates.
: 'If diff = 0 source and target are the same
: 'If diff = 1 source is newer than target
: 'If diff = -1 source is older than target
: diff = FileCompareFileDates(WFDSource, WFDTarget)
:
: 'if the dates, attributes and file times
: 'are the same...
: If (dwSourceFileSize = dwTargetFileSize) And _
: WFDSource.dwFileAttributes = WFDTarget.dwFileAttributes
: And _
: diff = 0 Then
:
: '...the files are the same, so take
: 'appropriate action (here, this is
: 'to simply list the files for info)
: List1.AddItem sTmp & vbTab & _
: dwSourceFileSize & vbTab & _
: WFDSource.dwFileAttributes & vbTab & _
: "files the same"
:
: List2.AddItem sTmp & vbTab & _
: dwTargetFileSize & vbTab & _
: WFDTarget.dwFileAttributes & vbTab & _
: "No"
:
: Else
:
: 'files are not the same
:
: If diff = 1 Then
: 'perform the preferred copy method ONLY if
: 'diff indicated that the source was newer!
: Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
: sTmp, False)
:
: sTargetMsg = "Yes"
: sSourceMsg = "source newer"
:
: ElseIf diff = -1 Then
: 'source is older
: sTargetMsg = "No"
: sSourceMsg = "source older"
:
: ElseIf diff = 0 Then
: 'the dates are the same but the file attributes
: 'are different. Since the date didn't change,
: 'replacing the file is a judgement call for
: 'the developer. Uncomment the line below if
: 'you want to copy this file, or alternatively,
: 'add a checkbox in your app the user can select
: 'to force an overwrite of files with similar dates.
: sTargetMsg = "No"
: sSourceMsg = "attr different"
: Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
: sTmp, False)
:
: End If
:
: 'debug only: add the files to the
: 'lists with the appropriate message
: List1.AddItem sTmp & vbTab & _
: dwSourceFileSize & vbTab & _
: WFDSource.dwFileAttributes & vbTab & _
: sSourceMsg
:
: List2.AddItem sTmp & vbTab & _
: dwTargetFileSize & vbTab & _
: WFDTarget.dwFileAttributes & vbTab & _
: sTargetMsg
:
: End If 'If dwSourceFileSize
:
: 'since the target file was found,
: 'close the handle
: Call FindClose(hTargetFile)
:
: Else:
:
: 'the target file was not found so
: 'copy the file to the target directory
: Call CopyFile(sSourceFolder & sTmp, sTargetFolder & sTmp,
: False)
:
: 'info only: add the files to the lists
: List1.AddItem sTmp & vbTab & _
: "target file did not exist"
:
: List2.AddItem sTmp & vbTab & _
: dwTargetFileSize & vbTab & _
: WFDTarget.dwFileAttributes & vbTab & _
: "Yes"
:
: End If 'If hTargetFile
: End If 'If WFDSource.dwFileAttributes
:
: 'clear the local variables
: dwSourceFileSize = 0
: dwTargetFileSize = 0
: 'added this code to show all files are selected
: If sTmp <> "." And sTmp <> ".." Then Text3.Text = Text3.Text + 1
:
: 'check Source and Target and Folder Name
: Debug.Print sSourceFolder & sTmp
: Debug.Print sTargetFolder & sTmp
:
: Loop While FindNextFile(hFileSource, WFDSource)
:
: End If
:
: End Function
:
: With the debug.print :-
: sSourceFolder = D:\My Photos\
: sTargetFolder = F:\BackUp My Photos\
: sTmp = Photos 2006
: There are no delimiters around this information
:
:
:

From: LondonLad on
Hi Kevin
Yes I agree I respect Randy Birch and his skill in vb program writing, so I
say what have I done for it not to work.
I would not say that I am messing with his program I am wanting to use it
but there is a problem I need help because my level does not match yours I
will learn from this if I can get help and complete the task and have a
backup project that checks the dates. What I want.
I have checked all of the folder information and its there as it should be
to my level if you can indicate a check that is perhaps not obvious I will
look and report back.
It goes through its routine without any reported errors or messages but when
after I check the new backup directory no folders have been written.
Folder name cannot start with \ / : * ? " <> | as I am sure you know so I am
not sure how to add "" to a folder name, but as a further point some of the
folder names do not have spaces and they are not being copied either.
If help is at hand I would be grateful.

Regards

"Kevin Provance" wrote:

> Top posted:
>
> Wow, when one can't even make Randy's code right off his site work...what
> does that tell ya? <g>
>
> Answer: Don't mess with things you don't understand and check out VB's
> "Move" statement.
>
> Do folders with spaces in them requires quotes? Check that out first. Are
> you checking for the existance of folders before moving files? *you* are
> responsible for checking for and creating folders...it's something you
> should be doing before *any* file operation. I would also throw in checks
> for the existence of files you are about to copy. It may seem redundant,
> but way stranger things have happened.
>
> Besides the ever annoying "it don't work" statement, are there any errors?
> error messages? Return values from the line of code that is failing? I'll
> tell you what I tell the simplest of customers I deal with: "it don't work"
> doesn't tell me a thing.
>
>
> "LondonLad" <LondonLad(a)discussions.microsoft.com> wrote in message
> news:C0AF8F79-8969-4FDB-ACDC-3F04F50AB5AA(a)microsoft.com...
> :
> : Hi
> :
> : The code is fromRandy Birch's CopyFileEx: Create a File Backup App
> : Since my earlier post this part of the code appears to do what I would
> : expect but I do not get the folder copied.
> : Can you help?
> :
> : Private Declare Function CopyFile Lib "kernel32" _
> : Alias "CopyFileA" (ByVal lpExistingFileName As String, _
> : ByVal lpNewFileName As String, ByVal bFailIfExists As Long) _
> : As Long
> :
> : Private Function BackupSourceFolder(ByVal hFileSource As Long, _
> : ByVal sSourceFolder As String, _
> : WFDSource As WIN32_FIND_DATA, _
> : ByVal sTargetFolder As String) As Long
> :
> : 'common local working variables
> : Dim sPath As String
> : Dim sRootSource As String
> : Dim sTmp As String
> : Dim sTargetMsg As String
> : Dim sSourceMsg As String
> : Dim diff As Long
> :
> : 'variables used for the source files and folders
> : Dim dwSourceFileSize As Long
> :
> : 'variables used for the target files and folders
> : Dim WFDTarget As WIN32_FIND_DATA
> : Dim hTargetFile As Long
> : Dim dwTargetFileSize As Long
> :
> : sRootSource = QualifyPath(sSourceFolder)
> : sPath = sRootSource & "*.*"
> :
> : 'last check!
> : If hFileSource <> INVALID_HANDLE_VALUE Then
> :
> : Do
> :
> : 'remove trailing nulls from the first retrieved object
> : sTmp = TrimNull(WFDSource.cFileName)
> :
> :
> :
> : 'if the object is not a folder.. CHANGED <> TO =
> : 'If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <>
> : FILE_ATTRIBUTE_DIRECTORY Then
> : If (WFDSource.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) =
> : FILE_ATTRIBUTE_DIRECTORY Then
> :
> :
> : 'check for the corresponding file
> : 'in the target folder by using the API
> : 'to locate that specific file
> : hTargetFile = FindFirstFile(sTargetFolder & sTmp, WFDTarget)
> :
> : 'if the file is located in the target folder..
> : If hTargetFile <> INVALID_HANDLE_VALUE Then
> :
> : 'get the file size for the source and target files
> : dwSourceFileSize = FileGetFileSize(WFDSource)
> : dwTargetFileSize = FileGetFileSize(WFDTarget)
> :
> : 'compare the dates.
> : 'If diff = 0 source and target are the same
> : 'If diff = 1 source is newer than target
> : 'If diff = -1 source is older than target
> : diff = FileCompareFileDates(WFDSource, WFDTarget)
> :
> : 'if the dates, attributes and file times
> : 'are the same...
> : If (dwSourceFileSize = dwTargetFileSize) And _
> : WFDSource.dwFileAttributes = WFDTarget.dwFileAttributes
> : And _
> : diff = 0 Then
> :
> : '...the files are the same, so take
> : 'appropriate action (here, this is
> : 'to simply list the files for info)
> : List1.AddItem sTmp & vbTab & _
> : dwSourceFileSize & vbTab & _
> : WFDSource.dwFileAttributes & vbTab & _
> : "files the same"
> :
> : List2.AddItem sTmp & vbTab & _
> : dwTargetFileSize & vbTab & _
> : WFDTarget.dwFileAttributes & vbTab & _
> : "No"
> :
> : Else
> :
> : 'files are not the same
> :
> : If diff = 1 Then
> : 'perform the preferred copy method ONLY if
> : 'diff indicated that the source was newer!
> : Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
> : sTmp, False)
> :
> : sTargetMsg = "Yes"
> : sSourceMsg = "source newer"
> :
> : ElseIf diff = -1 Then
> : 'source is older
> : sTargetMsg = "No"
> : sSourceMsg = "source older"
> :
> : ElseIf diff = 0 Then
> : 'the dates are the same but the file attributes
> : 'are different. Since the date didn't change,
> : 'replacing the file is a judgement call for
> : 'the developer. Uncomment the line below if
> : 'you want to copy this file, or alternatively,
> : 'add a checkbox in your app the user can select
> : 'to force an overwrite of files with similar dates.
> : sTargetMsg = "No"
> : sSourceMsg = "attr different"
> : Call CopyFile(sSourceFolder & sTmp, sTargetFolder &
> : sTmp, False)
> :
> : End If
> :
> : 'debug only: add the files to the
> : 'lists with the appropriate message
> : List1.AddItem sTmp & vbTab & _
> : dwSourceFileSize & vbTab & _
> : WFDSource.dwFileAttributes & vbTab & _
> : sSourceMsg
> :
> : List2.AddItem sTmp & vbTab & _
> : dwTargetFileSize & vbTab & _
> : WFDTarget.dwFileAttributes & vbTab & _
> : sTargetMsg
> :
> : End If 'If dwSourceFileSize
> :
> : 'since the target file was found,
> : 'close the handle
> : Call FindClose(hTargetFile)
> :
> : Else:
> :
> : 'the target file was not found so
> : 'copy the file to the target directory
> : Call CopyFile(sSourceFolder & sTmp, sTargetFolder & sTmp,
> : False)
> :
> : 'info only: add the files to the lists
> : List1.AddItem sTmp & vbTab & _
> : "target file did not exist"
> :
> : List2.AddItem sTmp & vbTab & _
> : dwTargetFileSize & vbTab & _
> : WFDTarget.dwFileAttributes & vbTab & _
> : "Yes"
> :
> : End If 'If hTargetFile
> : End If 'If WFDSource.dwFileAttributes
> :
> : 'clear the local variables
> : dwSourceFileSize = 0
> : dwTargetFileSize = 0
> : 'added this code to show all files are selected
> : If sTmp <> "." And sTmp <> ".." Then Text3.Text = Text3.Text + 1
> :
> : 'check Source and Target and Folder Name
> : Debug.Print sSourceFolder & sTmp
> : Debug.Print sTargetFolder & sTmp
> :
> : Loop While FindNextFile(hFileSource, WFDSource)
> :
> : End If
> :
> : End Function
> :
> : With the debug.print :-
> : sSourceFolder = D:\My Photos\
> : sTargetFolder = F:\BackUp My Photos\
> : sTmp = Photos 2006
> : There are no delimiters around this information
> :
> :
> :
>
> .
>