• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 147
  • Last Modified:

File Copy

Private Sub Command56_Click()
     
    Dim i8 As Integer
    Dim tra6 As Integer
    Dim Tem1 As String
    Dim Tem2 As String
     
    Form1.Adodc5.Refresh
   
    With Form1.Adodc5

         For i8 = 0 To .Recordset.RecordCount
               
                If Not .Recordset.EOF Then
                     
                     If Right(.Recordset.Fields(1).Value, 1) = "\" Then
                           Tem1 = Trim(.Recordset.Fields(1).Value) & Trim(.Recordset.Fields(3).Value)
                     Else
                           Tem1 = Trim(.Recordset.Fields(1).Value) & "\" & Trim(.Recordset.Fields(3).Value)
                     End If
                     
                     If Right(.Recordset.Fields(2).Value, 1) = "\" Then
                           Tem2 = Trim(.Recordset.Fields(2).Value) & Trim(.Recordset.Fields(3).Value)
                     Else
                           Tem2 = Trim(.Recordset.Fields(2).Value) & "\" & Trim(.Recordset.Fields(3).Value)
                     End If
                     
                     Call CopyFile(Trim(Tem1), Trim(Tem2))
                     
                     .Recordset.MoveNext
                     tra6 = tra6 + 1
                     
                End If
   
         Next i8

         If .Recordset.RecordCount = tra6 Then
             .Recordset.Requery
         End If

    End With
     
End Sub



Function CopyFile(sSource As String, sDestination As String) As Boolean
 
    On Error GoTo Err_CopyFile

    Dim bOk As Boolean
   
    bOk = False
   
    Call FileExists(sSource)
   
    MsgBox sSource
    MsgBox sDestination
   
    Call FileCopy(sSource, sDestination)
   
    bOk = True
   
    Exit_CopyFile:
    CopyFile = bOk
    Exit Function
   
    Err_CopyFile:
    Resume Exit_CopyFile

End Function

The code above copies filed well when I specify the path
as:

source: C:\My Document\Arrow.gif
Destination: C:\My Document\VB101\Backup\Arrow.gif

But when I specify the source and destination like this:

Source: c:\My Document\Menus\TwoGuys Wanna Rolla\Arrow.gif
Destination: C:\My Document\VB101\Backup\Arrow.gif

The Arrow.gif file is not coppied. My guess is the problem
is because of this TwoGuys Wanna Rolla.

The source and destination as well as the file name is stored in a database in the following style.

Source path | Destination path | Filename

How do I solve this problem. Your help is kindly appreciated.

Regards
Eugene



0
eugene007
Asked:
eugene007
  • 4
  • 3
  • 2
  • +1
1 Solution
 
Ryan ChongCommented:
Try this function to convert the filename to short form or vice versa:

Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal lBuffer As Long) As Long
Public Declare Function GetFullPathName Lib "kernel32" Alias "GetFullPathNameA" (ByVal lpFileName As String, ByVal nBufferLength As Long, ByVal lpBuffer As String, ByVal lpFilePart As String) As Long
Public Enum LongShortFile
    LongFile = 1
    ShortFile = 2
End Enum

Public Function GetFilePathName(ByVal OriginalFileName As String, Optional ByVal ConvertTo As LongShortFile = LongFile) As String
    On Error Resume Next
    Dim buffer As String, ret As Long
   
    If ConvertTo = LongFile Then
        'create a buffer
        buffer = Space(255)
        'copy the current directory to the buffer and append 'myfile.ext'
        ret = GetFullPathName(OriginalFileName, 255, buffer, "")
        'remove the unnecessary chr$(0)'s
        GetFilePathName = Left(buffer, ret)
    ElseIf ConvertTo = ShortFile Then
        'Create a buffer
        buffer = String$(165, 0)
        'retrieve the short pathname
        ret = GetShortPathName(OriginalFileName, buffer, 164)
        'remove all unnecessary chr$(0)'s
        GetFilePathName = Left$(buffer, ret)
    End If
End Function

cheers
0
 
Hornet241Commented:
I have no problem with this

src = "C:\Documents and Settings\Default User\Application Data\Microsoft\Internet Explorer\Brndlog.txt"

dst = "C:\Program Files\Common Files\Microsoft Shared\Database Replication\Conflict Viewer\Resources\1033\Brndlog.txt"

FileCopy src, dst

Otherwise
you could replace this
     For i8 = 0 To .Recordset.RecordCount
 with this
     Do While Not .Recordset.EOF
and the 'next i8' with 'Loop'

This could be causing the problem
     .Recordset.RecordCount -> returns 1
     For i8 = 0 To .Recordset.RecordCount -> 2 records

If the record that doesn't work is the only record that fits the select criteria then it will skip the the code at the 'If Not .Recordset.EOF Then' the 'DO' loop will prevent this and speed thing up by eliminating an 'If' statement


Also this

     Call FileCopy(sSource, sDestination)

can be changed to this

     FileCopy sSource, sDestination
0
 
Hornet241Commented:
Sorry Disreguard

This could be causing the problem
    .Recordset.RecordCount -> returns 1
    For i8 = 0 To .Recordset.RecordCount -> 2 records


Your 'If Not .Recordset.EOF' just skips the last record

This would get rid of the 'If Not .Recordset.EOF'

    For i8 = 0 To .Recordset.RecordCount - 1
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Hornet241Commented:
Sorry, Should have asked this to begin with, where is the error happening or is there an error?  Have you stepped through the code to see all steps happening?

0
 
eugene007Author Commented:
well I've placed msgbox n tried, the file name ok.

Only problem is the folders where the file is.

I've notice that folders with names like "My Dog Is James"

with spaces and the file in it cannot be coppied.

But folders with a single space like "My Document" The files in it for example Arrow.gif can be coppied.
0
 
eugene007Author Commented:
well I've placed msgbox n tried, the file name ok.

Only problem is the folders where the file is.

I've notice that folders with names like "My Dog Is James"

with spaces and the file in it cannot be coppied.

But folders with a single space like "My Document" The files in it for example Arrow.gif can be coppied.
0
 
Hornet241Commented:
I tried the multiple space folder name

src = "C:\Documents and Settings\Default User\Application Data\Microsoft\CLR Security Config\v1.0.3705\security.config"

dst = "C:\Windows Update Setup Files\security.config"

Call FileCopy(src, dst)

I had no problem again


Try this, comment out the 'On Error GoTo Err_CopyFile'
in the CopyFile function and see if it errors



0
 
eugene007Author Commented:
I found the solution.

Actually its very much related to my database.

The source field, I set as text with length 50.

But my folder name is larger than 50, so an Incomplete path
is stored in my database.

Therefore the file could not be copied.

Regards
Eugene
0
 
Ryan ChongCommented:
fine, please PAQ this question as well. if you want to PAQ this question with 0 pts or delete it, try post a 0 pts question at Community Support.
0
 
SpideyModCommented:
PAQ'd and points refunded per reqeust in http://www.experts-exchange.com/Community_Support/Q_20540878.html

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now