InstallShield利用环境变量得到INSTALLDIR,结合custom action来实现完全卸载

时间:2021-09-19 10:55:47

InstallShield 2012 LE版本中,

1. 自己定义了一个环境变量,叫做mypath,

2. 在custom action中,添加一个action,使用下面的代码来删除子文件夹和文件。

removeNew

Function removeNew()

	Set WshShell = CreateObject("WScript.Shell")
	Dim strFolder, intAnswer
	strFolder =  WshShell.ExpandEnvironmentStrings("%mypath%")
	strFolder = Left(strFolder, Len(strFolder) - 1)
	
	'strFolder = "C:\NewTemp"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFolders = objFSO.GetFolder(strFolder)
	
	MsgBox "The folder " & fldr & " exists so now to delete all folders inside.", vbInformation
	
	intAnswer = Msgbox("Delete all CATS folder and files?", vbYesNo, "Delete Files")
	If intAnswer = vbNo Then
		Exit Function
	End If
	
	recurse objFolders
	
	'delete the folder itself
	If objFSO.FolderExists(strFolder) Then
		objFSO.DeleteFolder(strFolder)
	End If
	
	Set objFSO = Nothing
	Set objFolders = Nothing
	Set WshShell = Nothing
	
End Function


sub recurse(byref objFolders)
	dim subfolders,files,folder,file
	set subfolders = objFolders.subfolders
	set files = objFolders.files
	for each file in files
		on error resume next
		name = file.name
		file.Delete True
	   If Err Then
		 MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
	   Else
		 MsgBox "Deleted:" & Name, vbInformation
	   End If
	   On Error GoTo 0
	next  
	for each folder in subfolders
		recurse folder
		If UBound(Split (folder.path, "\")) > 0 Then
		    name = folder.name
			folder.Delete True
			If Err Then
				MsgBox "Error deleting:" & Name & " - " & Err.Description, vbInformation
			Else
				MsgBox "Deleted:" & Name, vbInformation
			End If
			On Error GoTo 0
		End If 
	next   
	set subfolders = nothing
	set files = nothing
end Sub