View Full Version : copying files


Rockape
12-11-2007, 01:50 AM
Just a quick ask.

I'm trying create a routine using vba to copy file from one location to another eg from hard disk to floppy drive, usb memory etc.

can anyone please help?

Kind regards to all :(

tehNellie
12-11-2007, 02:37 AM
Have a look for FileSystemObject.....

Here's the one that shows a simple move/rename operation: http://www.access-programmers.co.uk/forums/showthread.php?t=139686&highlight=filesystemobject

Rockape
12-11-2007, 07:19 AM
Thanks,

I am a newbie and not quite sure what it means. What I'm basiically after is being able to click on a button as specified in the Event procedure, which will then copy the contents to a particular folder and replace them without any prompting that the file already exists.

Thanks again





Have a look for FileSystemObject.....

Here's the one that shows a simple move/rename operation: http://www.access-programmers.co.uk/forums/showthread.php?t=139686&highlight=filesystemobject

tehNellie
12-11-2007, 08:40 AM
Well the method in the link above allows you to iterate every file in the specified directory, change part of it's name, like the path for example, and then move it to the new filename. As long as the file isn't open somewhere the user wont get any prompts.

If you want to open a specific file instead then:


dim objfile as object, fso as object

Set fso = createobject("scripting.filesystemobject")
objfile = fso.GetFile("c:\testfile.txt")
' Move the file to \tmp directory.
objfile.Move ("c:\tmp\testfile.txt")
'copy the file
objfile.copy ("c:\tmp\testfile.txt")
'delete the file
objfile.delete


The FileSystemObject does exactly what it says on the tin, it allows you to manipulate File System Objects such as drives, directories and files and as a result is worth spending half an hour or so reading up on as pretty much anything you'd want to do with a file or bunch of files can be achieved simply with the fso.

The examples above aren't using Variables for file or directory names, but that is a perfectly valid way of passing information to and from the fso.

Rockape
12-12-2007, 03:51 AM
Thanks a lot. I will be testing it sometime today.

Will keep u posted :)

Regards




Well the method in the link above allows you to iterate every file in the specified directory, change part of it's name, like the path for example, and then move it to the new filename. As long as the file isn't open somewhere the user wont get any prompts.

If you want to open a specific file instead then:


dim objfile as object, fso as object

Set fso = createobject("scripting.filesystemobject")
objfile = fso.GetFile("c:\testfile.txt")
' Move the file to \tmp directory.
objfile.Move ("c:\tmp\testfile.txt")
'copy the file
objfile.copy ("c:\tmp\testfile.txt")
'delete the file
objfile.delete


The FileSystemObject does exactly what it says on the tin, it allows you to manipulate File System Objects such as drives, directories and files and as a result is worth spending half an hour or so reading up on as pretty much anything you'd want to do with a file or bunch of files can be achieved simply with the fso.

The examples above aren't using Variables for file or directory names, but that is a perfectly valid way of passing information to and from the fso.

Rockape
12-13-2007, 04:51 AM
hi,

tired the following in the event procedure

Private Sub Command0_Click()
Dim objfile As Object, fso As Object

Set fso = CreateObject("scripting.filesystemobject")
objfile = fso.GetFile("c:\1\test.txt")
' Move the file to c:\11 directory.
objfile.Move ("c:\11\test.txt")

End Sub

No luck.... note i'm using access97. I dont seem to get any help when i search for filesystemobject

Any clues?



Thanks a lot. I will be testing it sometime today.

Will keep u posted :)

Regards

Rockape
12-13-2007, 06:06 AM
Hi,

The code did work.. the file was moved but i got a runtime error 53 at the end. I think i have to do something at the end of the code but i have no clue!!!