Cannot delete folder within script

14 posts / 0 new
Last post
pcallan0
Offline
Joined: 02/21/2017 - 07:17
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's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

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
Offline
Joined: 02/21/2017 - 07:17

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's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

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
Offline
Joined: 02/21/2017 - 07:17

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
 

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Yes I believe you would need to delete all the files in the directory before removing it.

pcallan0
Offline
Joined: 02/21/2017 - 07:17

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's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

Not off hand but it shouldn't be too hard to create one, let me see if I can put one toghether.

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

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
Offline
Joined: 02/21/2017 - 07:17

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

Brian Element's picture
Brian Element
Offline
Joined: 07/11/2012 - 19:57

At the top of the function add this to define the object.

Dim objFSO as object

Pages