3 Replies Latest reply on Jun 25, 2018 2:43 PM by Peter Cranshaw

    How can I make deleting folders faster in Windows 10

    Peter Cranshaw

      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!

        • Re: How can I make deleting folders faster in Windows 10
          Josh Brady

          Possibly stupid question.  Is it possible that some machines are putting the temp files on the network vs local?

            • Re: How can I make deleting folders faster in Windows 10
              Peter Cranshaw

              No, I have it coded so that the tempfolder is specifically placed on the Windows desktop.

               

              '-----------------------------------------------------------------------'

              ' 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") & "\"

               

                  ' 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

               

              Function RandomString(Length As Integer)

               

                'PURPOSE: Create a Randomized String of Characters

                'SOURCE: www.TheSpreadsheetGuru.com/the-code-vault

                Dim CharacterBank As Variant

                Dim x As Long

                Dim str As String

               

                'Test Length Input

                If Length < 1 Then

                  MsgBox "Length variable must be greater than 0"

                  Exit Function

                End If

               

                CharacterBank = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", _

                  "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", _

                  "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", _

                  "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", _

                  "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")

               

                'Randomly Select Characters One-by-One

                For x = 1 To Length

                  Randomize

                  str = str & CharacterBank(Int((UBound(CharacterBank) - LBound(CharacterBank) + 1) * Rnd + LBound(CharacterBank)))

                Next x

               

                'Output Randomly Generated String

                RandomString = str

               

              End Function

               

            • Re: How can I make deleting folders faster in Windows 10
              Peter Cranshaw

              I think I've figured it out. I changed the ..

               

              DesktopPath = WshShell.SpecialFolders("Desktop") & "\"

               

              call to

               

              DesktopPath = WshShell.SpecialFolders("TemporaryFolder") & "\"

               

               

              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