Link to home
Start Free TrialLog in
Avatar of seahawks
seahawks

asked on

Copying files from one folder to another

Hello Everyone,

I am trying to copy files from one folder to another.  Everything I have tried I keep getting the same error.  The error is permission denied.  Here are the different things that I have tried:  
 
'Ex. Copy method
Dim fso As New Scripting.FileSystemObject, FileSource
 
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt")
FileSource.Copy ("E:\aLocalProjects\0152CSAS\Data\ASCII Target")
------------------------------------------------------    

'Ex. Move method
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt")
FileSource.Move ("E:\aLocalProjects\0152CSAS\Data\ASCII Target")
-------------------------------------------------------

'Ex. CopyFile method
fso.CopyFile "E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt", "E:\aLocalProjects\0152CSAS\Data\ASCII Target"
------------------------------------------------------

When I use:
Call FileCopy("E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt", "E:\aLocalProjects\0152CSAS\Data\ASCII

Target\e_prod.txt")

Now I get the error that the file is already open.  I take it that I need to close this how do I go
about this.



If you have any solutions to my problem i would greatly appreciate hearing back.

Thanks,
Kyle
ASKER CERTIFIED SOLUTION
Avatar of Maxim10553
Maxim10553
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ryan Chong
Hi seahawks,

try this:

Public Type SHFILEOPSTRUCT
     hwnd As Long
     wFunc As Long
     pFrom As String
     pTo As String
     fFlags As Integer
     fAnyOperationsAborted As Boolean
     hNameMappings As Long
     lpszProgressTitle As String
End Type

Public Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Public Const FO_COPY = &H2
Public Const FOF_ALLOWUNDO = &H40

Public Sub CopyFileWindowsWay(SourceFile As String, DestinationFile As String)
     Dim lngReturn As Long
     Dim typFileOperation As SHFILEOPSTRUCT

     With typFileOperation
        .hwnd = 0
        .wFunc = FO_COPY
        .pFrom = SourceFile & vbNullChar & vbNullChar 'source file
        .pTo = DestinationFile & vbNullChar & vbNullChar 'destination file
        .fFlags = FOF_ALLOWUNDO
     End With

     lngReturn = SHFileOperation(typFileOperation)

     If lngReturn <> 0 Then 'Operation failed
          IsSuccess = 1
     Else 'Aborted
          If typFileOperation.fAnyOperationsAborted = True Then
               MsgBox "Operation Failed", vbCritical Or vbOKOnly
          End If
     End If
End Sub

Use like:
CopyFileWindowsWay txtSourcePath, txtTargetPath

'Hope will help.
Did you try to copy it manually?

You might actually not have permissions to write. There might already be a file with same name which gets deleted. But you might not have rights to delete a file.

Please look into these things too.

And as suggested by "Maxim10553", use built-in functions as far as possible.
Avatar of seahawks
seahawks

ASKER

maxim,

When I use:
Call FileCopy("E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt", "E:\aLocalProjects\0152CSAS\Data\ASCII
Target\e_prod.txt")

Now I get the error that the file is already open.  I take it that I need to close this how do I go about this.

Thanks,
Seahawks
Do you have the file open for input or output? if so you can close it with close # and the # that the file is assigned to. If some other program is using this file, you would have to first get a handle to the file, and then close the handle accordinly, a complicated process. If this is a one time deal, restart your computer or at the least log out and back in and try the FileCopy function call again.
This might happen. Please see all of your previous attempts to get this working.

Is any of that part of code is still uncommented?

Especially the one below:

Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CSAS\Data\ASCII Source\e_prod.txt")
Thanks Maxim, your suggestion took care of my problem.

Thanks Again,
Seahawks
Hi Maxim10553,

You have stated in ur comment that  "you would have to first get a handle to the file, then close the handle accordinly, a complicated process". I am using some API call and I have the handle of the file. Can you tell me the process of closing the handle.
its urgent..... please reply soon....

Kirti