I'm writing a program (macro) to package drawings in my company into .ZIP files that include all the file formats that our different vendors need to work with them. To do that, it creates a temp folder and then use the "pack and go" functions in the soldiworks api to copy the files to that folder. With that it loops through the files and generates the different formats as needed. Once done it compress the contents of the tempfolder to a .ZIp file.
I've gotten the macro working pretty well. It cycles through all the files very neatly and generates the .ZIP file in short order.
The only problem that I have left is cleaning up afterwards. I need to get rid of the temp folder I made. On my own machine it happens reasonably fast. The tempfolder for a small assembly will take about 5 seconds to be deleted. But on other machines around my office that same folder will take as long as 2 minutes!
The one pattern I've noticed is that the fast machines are running Windows 7 and the slow machines are running Windows 10.
I've tried several different methods to remedy this, but the results have all been the same...
'----------------------------------------------------------------------'
' Delete the "tempfolder" '
'----------------------------------------------------------------------'
Sub DeleteTempFolder()
Dim fso As Object
' Get the basic Windows Filesystem objects
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder (tempfolder)
' Using VBA
' Kill tempfolder & "\*"
' RmDir tempfolder
' Using the Wscript.Shell
' Dim WshShell As Object
' Dim waitOnReturn As Boolean: waitOnReturn = True
' Dim windowStyle As Integer: windowStyle = 0
' Set WshShell = CreateObject("WScript.Shell")
' Run the command
' WshShell.Run "del " & Chr(34) & tempfolder & "\*" & Chr(34) & " /Q", windowStyle, waitOnReturn
' WshShell.Run "rmdir " & Chr(34) & tempfolder & "\" & Chr(34) & " /Q", windowStyle, waitOnReturn
End Sub
Does anyone have any suggestions how I can make removing that tempfolder faster on Windows 10?
Thanks!
I think I've figured it out. I changed the ..
DesktopPath = WshShell.SpecialFolders("Desktop") & "\"
call to
and the time required to delete the temp folder decreased dramatically.
I think I was up against Windows 10 indexing functions.
'-----------------------------------------------------------------------'
' Create a temporary folder on the Desktop with a Randomly created name '
'-----------------------------------------------------------------------'
Sub CreateTempFolder()
Dim fso As Object
Dim WshShell As Object
Dim DesktopPath As String
Dim sRString As String
Dim sTempFolder As String
' Get the basic Windows Filesystem objects
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
' Get the Windows Desktop path
' DesktopPath = WshShell.SpecialFolders("Desktop") & "\"
DesktopPath = WshShell.SpecialFolders("TemporaryFolder") & "\"
' Make an random 8 character foldername
sRString = RandomString(8)
' Create a folder on the desktop with the "sRString"
sTempFolder = fso.CreateFolder(DesktopPath & sRString)
' Save the folder name to the variable "tempfolder"
tempfolder = sTempFolder
End Sub