Results 1 to 9 of 9

Thread: Copying files in Vb

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    CT
    Posts
    124

    Cool Copying files in Vb

    I'm probably going about this the hard way, I was inputing a file and then writing it to a new dir, is their a simpler command something like

    copy "c:\file1" to "c:\temp\file2"

    that would work?
    Vini, Vidi, Vici!
    -----------------
    say this 5 times fast
    "I am not a pheasant plucker, I'm a pheasant plucker's son. I'm only plucking pheasants, till the pheasant plucker comes."

  2. #2
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    filecopy "c:\file1", "c:\temp\file2"

  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Or, if you do not want a copy in both places, use Name to rename the file.

    Name "c:\file1" As "c:\temp\file2"

  4. #4
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    Or, if you are copying to a location where the file aready exists, (using name here will raise an error), you can use:

    VB Code:
    1. '==========================================
    2. 'for the value, bFailIfExists.  If you set this value to true, then
    3. 'the file won't be copied is the file already exists.  If this value
    4. 'is false, then the file will overwrite any existing copies of the
    5. 'file
    6. '===========================================
    7.  
    8. Private Declare Function CopyFile Lib "kernel32" Alias _
    9.                         "CopyFileA"  (ByVal lpExistingFileName As String, _
    10.                          ByVal lpNewFileName As String, _
    11.                          ByVal bFailIfExists As Long) As Long
    12.  
    13.  
    14. Private Command1_Click()
    15. Dim lret as long
    16.  
    17. '===========================================
    18. 'This will create a duplicate file, without deleting the original
    19. 'file.  And will overwrite the destination file, if it already exits.
    20. '===========================================
    21. lret=CopyFile("c:\windows\desktop\test.txt", "c:\windows\desktop\test1.txt", True)
    22.  
    23. End sub

    Hope this helps.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    CT
    Posts
    124
    ok this is what I'm trying and I'm getting an error
    'Path not found'

    VB Code:
    1. FileCopy App.Path & "\" & File1.Filename, App.Path & "\backup\" & TempFilename


    File1.Filename is from a filelistbox, and is the file name we are copying.
    Tempfilename is the newname of the file.
    Vini, Vidi, Vici!
    -----------------
    say this 5 times fast
    "I am not a pheasant plucker, I'm a pheasant plucker's son. I'm only plucking pheasants, till the pheasant plucker comes."

  6. #6
    Hyperactive Member
    Join Date
    May 2000
    Location
    Or
    Posts
    316
    I am going to guess that the directory that you want to copy to, doesn't exist. In all the commands that have been posted, the directory structure must be present for the copy commands to work. If you need to check to see if a nested directory exists (and create it if it doesn't) then look at this tip from vbworld:

    http://www.vbworld.com/files/tip528.html

    Hope this helps.

  7. #7
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Right. If backup directory does not exist, you will get Path Not Found. So, either create it outside of the VB app, or use MkDir in VB to make sure it exists.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    CT
    Posts
    124
    humm, that's not the problem I was thinking that I possibly could of writen the code improperly,

    lets just say that the main directory is c:\

    the backupdir would then be c:\backup.

    which is how I have it set up, so I was thinking that the way I was linking all the commands

    App.Path & "\backup\" & TempFilename

    the & commands weren't working correctly, I'll take a look at that tip though.
    Vini, Vidi, Vici!
    -----------------
    say this 5 times fast
    "I am not a pheasant plucker, I'm a pheasant plucker's son. I'm only plucking pheasants, till the pheasant plucker comes."

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2001
    Location
    CT
    Posts
    124
    ok narrowed it down to this part of the code.

    TempFilename

    this is what is causing the error,

    I added a msgbox to display the last section of where it's being copied to and it's not showing the file name, just the dir.


    Temppath = App.Path & "\backup\" & TempFilename
    MsgBox Temppath

    = c:\backup\

    no file name??

    Ok, I found my problem, I didn't declare Tempfilename in the option explicit area, but under a differend command_click()

    opps.
    My bad. Thanks all.
    Vini, Vidi, Vici!
    -----------------
    say this 5 times fast
    "I am not a pheasant plucker, I'm a pheasant plucker's son. I'm only plucking pheasants, till the pheasant plucker comes."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width