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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOS Copy Function

Status
Not open for further replies.

clifftech

ISP
Nov 9, 2001
111
US
I need a macro for Excel that will allow me to copy files from one directory to another. The macro will have three parameters: the file name to copy, the current path of the file, and the path where the file is to be copied.

Any help would be appreciated.

 

Look at the FileCopy, it is what you need.


Have fun.

---- Andy
 
Code:
Sub Copy_File()
   Dim file_name As String, source_path As String, target_path As String
   
   file_name = "Search.xls"
   source_path = "C:\Documents and Settings\WinblowsME\Desktop"
   target_path = "C:\Documents and Settings\WinblowsME\Desktop\Temp\"
   
   Call File_Copy(file_name, source_path, target_path)
End Sub

Private Sub File_Copy(file_name As String, source_path As String, target_path As String)
   If Right(source_path, 1) <> "\" Then: source_path = source_path & "\"
   If Right(target_path, 1) <> "\" Then: target_path = target_path & "\"
   
   On Error GoTo PRINTERR
   
   FileCopy source_path & file_name, target_path & file_name
   Exit Sub
PRINTERR:
   MsgBox Err.Description
End Sub
 
I also found this example of copying files:

Sub CopyFile()
Dim fso
Dim file As String, sfol As String, dfol As String
file = "test.xls" ' change to match the file name
sfol = "C:\" ' change to match the source folder path
dfol = "E:\" ' change to match the destination folder path
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(sfol & file) Then
MsgBox sfol & file & " does not exist!", vbExclamation, "Source File Missing"
ElseIf Not fso.FileExists(dfol & file) Then
fso.CopyFile (sfol & file), dfol, True
Else
MsgBox dfol & file & " already exists!", vbExclamation, "Destination File Exists"
End If
End Sub

This script also provides the warnings that I need.

What is the best way to copy a list of files from a column in the spreadsheet?

Thanks for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top