?
Solved

File Copy

Posted on 2003-02-25
10
Medium Priority
?
146 Views
Last Modified: 2010-04-07
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
Comment
Question by:eugene007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8023568
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
 
LVL 3

Expert Comment

by:Hornet241
ID: 8023609
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
 
LVL 3

Expert Comment

by:Hornet241
ID: 8023623
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Hornet241
ID: 8023634
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
 

Author Comment

by:eugene007
ID: 8024753
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
 

Author Comment

by:eugene007
ID: 8024891
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
 
LVL 3

Expert Comment

by:Hornet241
ID: 8028942
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
 

Author Comment

by:eugene007
ID: 8032176
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
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8032227
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
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 8079775
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] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question