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!

need help copying files from one folder to another

Status
Not open for further replies.

sm2605

Programmer
Nov 19, 2003
44
US
Hi, i wrote this code as below but i keep getting a file not found error. I know the file exists, but im assuming there is something wrong with the code. By the way i am trying to copy all the files from the temp folder located in a datadirectory to the source of the dataDirectory.

fileSysObject.CopyFile dataDirectory & "\Temp", dataDirectory, overwrite
 
Im sorry but im not familiar with late binding. I was only told my a coworker that i was going to get an error if the user didnt have the proper add-ins when i use file sys object function. This is why im asking if there is a way to copy files using a function or class. How about something like this:

while folder is not "temp folder"
copy file to temp folder
go to next file
loop

something like that. Im not sure if i need to specisfy a particular file name because there are about 500 files to be copied.
 
Sorry,

To use Early Binding, you would set a reference to the Microsoft Scriping Runtime dll in your project. You would then dimension it something like this:

Dim fso As FileSystemObject

Late binding means to declare it as a generic object and let the system resolve it at runtime. There is a slight performance hit for late binding. So you would do someting like this:

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

The above will work if they have ScrRun.dll on their machine, otherwise it will still break. To avoid that, add some error checking like this:

Dim fso As Object
On Error Resume Next 'Handle errors here
Set fso = CreateObject("Scripting.FileSystemObject")
If err.Number Then 'Cannot use FileSystemObject
Err.Clear
'Do alternate coding using Dir function
Else
'Use your new code here
End If
On Error GoTo 0 'Reset error handling

Good Luck!


Have a great day!

j2consulting@yahoo.com
 
thanks, but when you say use dir code how is that helping the code, because dir functions accept two arguments and returns a string with the name of a file or folder that matches the specified folder or file attribute. Ive tried it anyway incase im misunderstanding it. here is what i have so far:


Private Sub Form_Load()
If getKey("software\financial\Paths\User Path") = Null Then
MsgBox ("software not found")
Else
Dim dataDirectory As String
Dim customerFile
Dim fileSysObject

dataDirectory = getKey("software\financial\Paths\User Path")
MsgBox (dataDirectory)
If Err.Number Then 'cannot use fileSystemobject
Err.Clear
fileSysError = Dir(dataDirectory & "\*.*", vbNormal)
Else

If Not fileSysObject.FolderExists(dataDirectory & "\OTPTemp") Then
Set fileSysObject = CreateObject("Scripting.FileSystemObject")
MsgBox ("folder doesn't exist")
'Temp doesn't exist, create Temp folder
MkDir (dataDirectory & "\Temp")
'copy files from data directory to temp folder
fileSysObject.CopyFiles dataDirectory & "\*.*", dataDirectory & "\Temp\", True
Else
MsgBox ("Temp folder already exists")
 
Copy files from one Dir to another, selected filepattern, NO FSO :)

'"*.*" all files
'"*.txt" text files
'"*.exe" you get the idea!

Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long

Private Sub Command1_Click()
CopyFiles "c:\", "c:\a\", "*.*"
End Sub

Private Sub CopyFiles(SourceDir As String, DestDir As String, ThisPattern As String)
On Error GoTo ErrHandler
Dim Thisfile As String
Dim arr() As String
Dim x As Integer

If Right$(SourceDir, 1) <> &quot;\&quot; Then SourceDir = SourceDir + &quot;\&quot;
If Right$(DestDir, 1) <> &quot;\&quot; Then DestDir = DestDir + &quot;\&quot;

Thisfile = Dir$(SourceDir + ThisPattern, 0)


Do While Thisfile <> &quot;&quot;
ReDim Preserve arr(x)
arr(x) = Thisfile
x = x + 1
Thisfile = Dir$
Loop

MakeDir (DestDir)

For x = 0 To UBound(arr())
FileCopy SourceDir & arr(x), DestDir & arr(x)
Next x
Exit Sub
ErrHandler:
MsgBox &quot;An Error Occured: &quot; & Err.Number & &quot; &quot; & Err.Description
End Sub

Public Function MakeDir(strPath As String) As Boolean
If Right(strPath, 1) <> &quot;\&quot; Then
strPath = strPath & &quot;\&quot; 'Make sure ends with &quot;\&quot;
End If
MakeSureDirectoryPathExists (strPath) 'Make the dir
If Dir(strPath, vbDirectory) <> &quot;&quot; Then MakeDir = True 'Check dir exists
End Function 'returns true/false
 
Im sorry but im kinda lost at the code above. What exactly is it doing and how am i using this?

thanks again
 
is this my question on whther or not i can create a function and just call it. if so how do i use this and how do i get rid of the current code i have?

thanks for your help :)
 
Hello sm2605,

Sorry, not trying to ignore you. I would recommend trying the above code in a new project. Just single step through it until you get a feel for what it is doing.

I was a little bit unclear about what you are trying to do with this code:

If getKey(&quot;software\financial\Paths\User Path&quot;) = Null Then

1. If you want to use FileSystemObject if available then try something like this with late binding:

a. In declarations area
Private mfso As Object 'Late binding will resolve at runtime

b. Form Load code

On Error Resume Next
Set mfso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If err.Number Then 'Cannot use FileSystemObject
err.Clear
End If

c. Click code

If mfso Is Nothing Then
'Use LPlates code above which uses Dir command
Else
'Use code with FileSystemObject
End If

The above allows you to handle either situation in the field without causing a problem. You may decide you only want to do the DIR piece since it is always available. I hope this helps you out! Good Luck!


Have a great day!

j2consulting@yahoo.com
 
Hi sm2605 & SBendBuckeye

To use the above code simply call it like so...

CopyFiles dataDirectory, dataDirectory & &quot;\Temp\&quot;, &quot;*.*&quot;

Which will copy all files from 'datadirectory' to datadirectory & &quot;\Temp&quot;

The last flag is for filetypes, if you wish to copy all files use &quot;*.*&quot; If you wish to copy text files use &quot;*.txt&quot; etc!



 
Thanks for your help!! i dont want to use FSO because it might not be available for some users. I will try and run tho=rough the above code in a new project and see what happens.

basically when i say If getKey(&quot;software\financial\Paths\User Path&quot;) = Null Then
im just trying to see if the software exists by looking into the registery, and if it does then continue on with the rest of my code.

I already have the dll registered so i can use FSO, how do i remove it so i am able to test the code written above without the FSO?

wow!!! thanks so much for your time.
 
In the VB editor, click Projects and then References. If you have selected in the list uncheck it and you are good to go. If you never selected it, it will have no impact on your program so there is no need to unregister it.

Good LucK!

Have a great day!

j2consulting@yahoo.com
 
iwas trying to run the code that LPlates wrote up above but i get an error on the first line:

Private Declare Function MakeSureDirectoryPathExists Lib &quot;imagehlp.dll&quot; (ByVal lpPath As String) As Long

of only comments may appear after end sub
 
Declarations go in the Declaration section at the start of a module

________________________________________________________________
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?'
 
Don't be discouraged, we all were at one time or another.

Have a great day!

j2consulting@yahoo.com
 
Ok Ive added comments to the code from above. Some of then comments im not sure of. Can someone check it out and let me know if the comments are correct.

Option Explicit
Private Declare Function MakeSureDirectoryPathExists Lib &quot;imagehlp.dll&quot; (ByVal lpPath As String) As Long

Private Sub Form_Load()
'Copy files from one Dir to another, selected filepattern, NO FSO

'&quot;*.*&quot; all files
'&quot;*.txt&quot; text files
'&quot;*.exe&quot; 'NOT SURE WHATS SUPPOSED TO GO IN HERE
End Sub
Private Sub Command1_Click()
CopyFiles &quot;c:\&quot;, &quot;c:\a\&quot;, &quot;*.*&quot;
End Sub

Private Sub CopyFiles(SourceDir As String, DestDir As String, ThisPattern As String)
On Error GoTo ErrHandler
Dim Thisfile As String
Dim arr() As String 'DONT KNOW WHAT THIS DOES
Dim x As Integer

If Right$(SourceDir, 1) <> &quot;\&quot; Then SourceDir = SourceDir + &quot;\&quot; 'does source directory have a &quot;\&quot; if not then add it
If Right$(DestDir, 1) <> &quot;\&quot; Then DestDir = DestDir + &quot;\&quot; 'does desination directory have a &quot;\&quot; if not then add it

Thisfile = Dir$(SourceDir + ThisPattern, 0) 'the file being copied is from the sourceDit and put in ThisFile


Do While Thisfile <> &quot;&quot; 'loop to go through each file in the source directory
ReDim Preserve arr(x)
arr(x) = Thisfile
x = x + 1
Thisfile = Dir$
Loop

MakeDir (DestDir) 'call makedirectory function

For x = 0 To UBound(arr())
FileCopy SourceDir & arr(x), DestDir & arr(x) 'loop until no more copy source into destination
Next x
Exit Sub
ErrHandler:
MsgBox &quot;An Error Occured: &quot; & Err.Number & &quot; &quot; & Err.Description
End Sub

Public Function MakeDir(strPath As String) As Boolean 'makedirectory function NOT SURE WHAT THIS DOES
If Right(strPath, 1) <> &quot;\&quot; Then
strPath = strPath & &quot;\&quot; 'Make sure ends with &quot;\&quot;
End If
MakeSureDirectoryPathExists (strPath) 'Make the dir
If Dir(strPath, vbDirectory) <> &quot;&quot; Then MakeDir = True 'Check dir exists
End Function 'returns true/false
End Function

 
Dim arr() As String 'DONT KNOW WHAT THIS DOES

This line is dimensioning a dynamic string array. As of this point we don't know how big it may be. It is resized further down with this:

ReDim Preserve arr(x) 'Resize and save contents
arr(x) = Thisfile 'Assign current value
x = x + 1 'Increment index for next loop

Good Luck!


Have a great day!

j2consulting@yahoo.com
 
Thanks!!
What am i supposed to put in the FormLoad(). Is that where the beginning of my code goes...meaning everything else that ive coded. And can i call this page rather then copy it into the program from the previous page?

Last question, Command1_Click() what is the purpose of that?

thanks again
 
Anything you put in Form Load will be created when the form is opened which means it will be available to you whenever you need it. You may not need to put anything there.

If you need this code to be executed when the user requests it then you would probably put it in the click event of a command button.

Many times a programmer will just put the code on a button's click event for testing. If you create a new form and paste a command button on the form from the tool menu the first one will be named Command1.

Good Luck!

Have a great day!

j2consulting@yahoo.com
 
THanks!!! so much what about this function:

why is it being used and what is it testing for? It is creating a directory but what directory is it creating?

Public Function MakeDir(strPath As String) As Boolean 'makedirectory function NOT SURE WHAT THIS DOES
If Right(strPath, 1) <> &quot;\&quot; Then
strPath = strPath & &quot;\&quot; 'Make sure ends with &quot;\&quot;
End If
MakeSureDirectoryPathExists (strPath) 'Make the dir
If Dir(strPath, vbDirectory) <> &quot;&quot; Then MakeDir = True 'Check dir exists
End Function 'returns true/false
 
Public Function MakeDir(strPath As String) As Boolean 'makedirectory function

Whatever path you pass to this function, ie: the strPath, will be created, that was the example Command1_Click you asked about before, it was a demonstration on how to call this function.

Basically if you want to make a directory such as &quot;C:\A\B\C\D&quot; then you call this function like so...

MakeDir &quot;C:\A\B\C\D&quot;

Have fun!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top