Skip to main content

Cannot delete folder within script

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
 

Brian Element Wed, 03/20/2019 - 09:03

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.

pcallan0 Wed, 03/20/2019 - 09:44

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
 

Brian Element Wed, 03/20/2019 - 09:51

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 

pcallan0 Wed, 03/20/2019 - 10:13

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
 

pcallan0 Wed, 03/20/2019 - 10:27

Hi Brian,
Thanks again.
Is there any chance that you've a script that will read the contents of a folder and delete all?
Phil
 
 

Brian Element Wed, 03/20/2019 - 10:38

In reply to by pcallan0

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

pcallan0 Wed, 03/20/2019 - 10:47

Hi brian,
I'm getting a  vriable not defined (TRUE) error
Phil