Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy File 1

Status
Not open for further replies.

DSTR3

Technical User
Jan 21, 2005
41
US
Hi,
I'm new to VB...I'm using VB6. I need a command button to copy a file and place it into another directory...

Copy C:\ProServ\DB\PS.mdb to C:\ProServ\BackUp

It has to rename the file from PS.mdb to BU.Mdb and overwrite the previous BU.mdb file.

Any help appreciated.
Thanks
DS
 
Look at the FileCopy statement in your VBHelp

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Ok Thanks, I'm doing this and it works fine.
FileCopy "c:\Proserv\DB\DellSat.mdb", "C:\ProServ\DB\BU.mdb"
MsgBox "Completed"
A quick question though. Does it Overwrite the exsisting file? How exactly does it work?
Thanks
DS
 
The code you are using will not overwrite the existing file.

See the Name statement in VB6 Help

Name Statement


Renames a disk file, directory, or folder.

Syntax

Name oldpathname As newpathname

The Name statement syntax has these parts:

Part Description
oldpathname Required.String expression that specifies the existing file name and location — may include directory or folder, and drive.
newpathname Required. String expression that specifies the new file name and location — may include directory or folder, and drive. The file name specified by newpathname can't already exist.


Remarks

The Name statement renames a file and moves it to a different directory or folder, if necessary. Name can move a file across drives, but it can only rename an existing directory or folder when both newpathname and oldpathname are located on the same drive. Name cannot create a new file, directory, or folder.

Using Name on an open file produces an error. You must close an open file before renaming it. Namearguments cannot include multiple-character (*) and single-character (?) wildcards.

HTH Hugh
 
I could have given you misleading advise there. If is working as you have coded it be happy.

ref to the help for the Name statement could be useful though!

regards Hugh,
 
Hugh
The help file I pointed to references the Name statement and the Kill statement, which should cover all points

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Thanks, It shouldn't be this hard. I thought that by doing this it would just overwrite the mdb file.
Private Sub Command1_Click()
FileCopy "c:\Proserv\DB\DellSat.mdb", "C:\ProServ\DB\BU.mdb"
Unload Me
End
End Sub
DS
 
I draw your attention again to faq222-2244 paragraph 8 and the Kill statement pointed out above.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
In my book, the best way to know is to try and see.

If FileCopy does not overwrite it, try:
Code:
Private Sub Command1_Click()
    [b]Kill "C:\ProServ\DB\BU.mdb"[/b]
    FileCopy "c:\Proserv\DB\DellSat.mdb", _
         "C:\ProServ\DB\BU.mdb"
    Unload Me
    [green]'End - bad idea, NEVER needed[/green]
End Sub

HTH

---- Andy
 
You could use a FileSystemObject (you need a reference to Microsoft Scripting Runtime)
Code:
With New FileSystemObject
   .CopyFile "c:\Proserv\DB\DellSat.mdb", "C:\ProServ\DB\BU.mdb", True
End With
Where thr "True" argument specifies over-writing of files.
 
Well, the FileCopy doc on the MSDN site fails to say explicitly that a file will be overwritten, although it implies it. So, yes, DSTR3, your file should overwrite. If you want to test it, and you can't do it by checking the mdb files for some reason, then create a couple of text files and play with them.

HTH

Bob
 
Try adding the Date time stamp to the file name, if you want to create unique filenames and dont want to delete previous backups.

------------------------------------------
The faulty interface lies between the chair and the keyboard.
 
That sounds goog, how do you add a datestamp?
Thanks
DS
 
So it would be like this?
Date().Mdb
Thanks
DS
 
I think what BobRodes suggests is put date and time as a part of your file name, but I don't think that's what you wanted to have.

You would:
Code:
FileCopy "c:\Proserv\DB\DellSat.mdb", _
   "C:\ProServ\DB\" & Date & ".mdb"
so you would end up with file
"C:\ProServ\DB\11282007.mdb"

You may also add a Time to your file name the same way.

But if you always want to have BU.mdb that will not work for you since you would have different file name every time you run this code.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top