Cannot delete folder within script
Forums
Hi Brian,
I'm writing a script that creates a working folder, does a lot of file compilation within that folder, and then moves the resulting two files to a new folder.
Once the results are moved to the new folder I then want to delete the working folder.
As part of the main subroutine I have the folder name defined much earlier in the script as shown below
Set fldrName as String
Set wD as String
wD= CurDir()
fldrName = "CRISP WORKING FOLDER"
However, the following extract from the script keeps returning an error saying the folder doesn't exist.
Function DeleteCrispWorkingFolder(wD As String, errSwitch As Boolean)
If errSwitch = 0 Then On Error GoTo ErrHandler
Dim task As Object
Dim errMsg As String
Client.CloseAll
Set task = Client.ProjectManagement
task.DeleteFolder wD & "\CRISP WORKING FOLDER"
Set task = Nothing
Client.RefreshFileExplorer
Exit Function
ErrHandler:
If Client.ErrorCode > 0 Then
errMsg = "Error: " & Client.ErrorString
MsgBox errMsg, MB_OK, "IDEAScript Error"
Client.Quit
Else
errMsg = "Error # " & Str(Err.Number) & Chr$(13) & Err.Description & Chr$(13)
MsgBox errMsg, MB_OK, "IDEAScript Error"
Client.Quit
End If
End Function
Any help would be greatly appreciated.
Thanks a million,
Phil
Hi Brian,
Hi Brian,
Thanks for your prompt reply as always.
I had tried
task.DeleteFolder "CRISP WORKING FOLDER"
I also tried specifying the fldrNme in the function line
Function DeleteCrispWorkingFolder(fldrNme As String, errSwitch As Boolean)
and then
task.DeleteFolder fldrNme
Unfortunately, IDEA keeps telling me the folder doesn't exist
Could it be that I have defined the fldrNme as a string far earlier in the script, and somehow IDEA won't release it?
Thanks again,
Phil
Is the fldrNme defined as a
Is the fldrNme defined as a global variable? It is defined outside of the function so it needs to be defined as a global.
Also you might want to just try this short code to see if this deletes the folder. Make sure that the folder is empty before trying to delete it.
Function DeleteCrispWorkingFolder(wD As String, errSwitch As Boolean)
Set task = Client.ProjectManagement
task.DeleteFolder "CRISP WORKING FOLDER"
Set task = Nothing
End Function
Hi Brian,
Hi Brian,
The fldrNme is a globale variable and is used in multiple functions up to the point where I want to delete the folder.
The short code didn't do it, but I didn't realise the folder should be empty which may be the cause of my problems.
The number of working files in the folder depends on the job and will vary widely.
I'm guessing from your reponse I'll need to read the contents, and then delete all files before deleting the folder?
Thanks
Phil
Here is some code. The
Here is some code. The IDEATest is the name of the folder I used to delete so just replace that with your folder.
Sub Main
Call deleteFilesInFolder("IDEATest")
End Sub
Function deleteFilesInFolder(sFolder As String)
Const DeleteReadOnly = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(sFolder & "\*.*")
objFSO.DeleteFolder(sFolder)
Set objFSO = Nothing
End Function
Try not including the path
Try not including the path information. It looks like this only works for folders in the project folder. I tried an example without the path informaton and it works but when I add the path info I get the same error as you do.