Move and deleting files

G

Guest

I need some assistance
I have this database that has to import 35 files everyday. NOw for checking
purpose, i have this code that once it imports all the files, it deletes all
files from that folder so user can't upload same files
I need to modify the code to first move the files first to a folder of my
choice and then delete. Please help

code:
Function DeleteFiles()
Dim fso As New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Dim fil As Scripting.File

Set fld = fso.GetFolder([Forms]![Protobase Upload]![Pathtxt])

For Each fil In fld.Files
fil.Delete
Next fil

Set fso = Nothing
Set fld = Nothing
Set fil = Nothing

End Function
 
D

Douglas J. Steele

To move a file, you use the MoveFile method. There's a sample at
http://msdn.microsoft.com/library/en-us/script56/html/277621e8-9f65-4500-bc35-89610894a3cf.asp

However, there's no real reason to use FSO for what you're trying to do. The
following uses strictly build-in VBA methods:

Function DeleteFiles()
Dim strFile As String
Dim strFolder As String
Dim strTargetFolder As String

strFolder = [Forms]![Protobase Upload]![Pathtxt]
If Right(strFolder, 1) <> "\" Then
strFolder = strFolder & "\"
End If

strTargetFolder = "C:\My Data\Imported Files\"

strFile = Dir(strFolder & "*.*")
Do While Len(strFile) > 0
Name strFolder & strFile As strTargetFolder & strFile
strFile = Dir()
Loop

End Function

If strFolder and strTargetFolder aren't on the same drive, you can't use the
Name statement. In that case, you'll have to use FileCopy, and then delete
the file:

FileCopy strFolder & strFile, strTargetFolder & strFile
Kill strFolder & strFile
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top