PDA

View Full Version : Why does this code move rather than copy files?



agarwaldvk
01-16-2008, 02:56 PM
Hi everybody

Sorry, guys. I did do further tests on it and it does actually copy the files and not move them.

So far so good but when the code for deleting the folder is executed, it first deletes all the files therein - that is why the folder is not empty (I see no files in it) but for some reason it still sees as being used and hence doesn't permit it to be deleted.

Any suggestions, please!



Dim objFile As File, count1 As Long
Dim fso As Object, localPCObjParentFolder As Object
Dim objFolder As Folder, networkObjParentFolder As Folder
Dim localPCParentFolderName As String, networkParentFolderName As String
Dim strSourceFolder As String, strDestFolder As String, movingFilesFilename As String, logFilePath As String
movingFilesFilename = "SASCodeToMoveFiles.sas"
Set fso = CreateObject("Scripting.FileSystemObject")


strSourceFolder = locationPath & thisMonthFolderName & "\" & htmlOutputFilesFolder
'This results in : "D:\SingleDataSource_M03817\2007-12\HTML_Outputs"

strDestFolder = userSelectedBaseFolderName & thisMonthFolderName & "\" & htmlOutputFilesFolder

'This results in : "J:\DDS\Deepak\Single Repository For Source Data\2007-12\HTML_Outputs"

Set objFolder = fso.GetFolder(strSourceFolder)

For Each objFile In objFolder.Files
objFile.Copy strDestFolder & "\" & objFile.Name
Next objFile 'go to the next file


fso.DeleteFolder objFolder, True 'Error here - Files are moved rather copied and get Permission Denied



The above code, whilst intended to copy files from a folder in the local "D:\" drive to a folder on the network drive, seems to move the files rather than copy them. As a concequence, I am not then able to delete the "From" folder since it complains about "Permission Denied".

Whe this code is run without a lot of processing, it seems to work ok but when run after a fair amount of processing using VBA Excel, it starts to move rather than copy files. Is this pointing to a memory problem?

I actually changed the code from initially moving these files to copying these files because of the "Permission Denied" problem. For some reason, it somehow seems to think that the referred to folder (for deletion) is still being used.

Any idea what to do next???????????



Best regards



Deepak

tpoynton
01-16-2008, 06:30 PM
I havent seen this in VBA, but I have in windows; it happens when something else is using the file/folder.

Try closing it first?

objfolder.close
objfile.close
fso.deletefolder ...

I dont do much file stuff in VBA, but this is worth trying since it's been a few hours. If nothing else, someone will tell me I'm wrong and provide the right answer!