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\0152CS AS\Data\AS CII Source\e_prod.txt")
FileSource.Copy ("E:\aLocalProjects\0152CS AS\Data\AS CII Target")
-------------------------- ---------- ---------- --------
'Ex. Move method
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CS AS\Data\AS CII Source\e_prod.txt")
FileSource.Move ("E:\aLocalProjects\0152CS AS\Data\AS CII Target")
-------------------------- ---------- ---------- ---------
'Ex. CopyFile method
fso.CopyFile "E:\aLocalProjects\0152CSA S\Data\ASC II Source\e_prod.txt", "E:\aLocalProjects\0152CSA S\Data\ASC II Target"
-------------------------- ---------- ---------- --------
When I use:
Call FileCopy("E:\aLocalProject s\0152CSAS \Data\ASCI I Source\e_prod.txt", "E:\aLocalProjects\0152CSA S\Data\ASC II
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
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
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CS
FileSource.Copy ("E:\aLocalProjects\0152CS
--------------------------
'Ex. Move method
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CS
FileSource.Move ("E:\aLocalProjects\0152CS
--------------------------
'Ex. CopyFile method
fso.CopyFile "E:\aLocalProjects\0152CSA
--------------------------
When I use:
Call FileCopy("E:\aLocalProject
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
maxim,
When I use:
Call FileCopy("E:\aLocalProject s\0152CSAS \Data\ASCI I Source\e_prod.txt", "E:\aLocalProjects\0152CSA S\Data\ASC II
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
When I use:
Call FileCopy("E:\aLocalProject
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\0152CS AS\Data\AS CII Source\e_prod.txt")
Is any of that part of code is still uncommented?
Especially the one below:
Set FileSource = fso.GetFile ("E:\aLocalProjects\0152CS
ASKER
Thanks Maxim, your suggestion took care of my problem.
Thanks Again,
Seahawks
Thanks Again,
Seahawks
yw
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
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
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(SourceF
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(typFileOpe
If lngReturn <> 0 Then 'Operation failed
IsSuccess = 1
Else 'Aborted
If typFileOperation.fAnyOpera
MsgBox "Operation Failed", vbCritical Or vbOKOnly
End If
End If
End Sub
Use like:
CopyFileWindowsWay txtSourcePath, txtTargetPath
'Hope will help.