Results 1 to 3 of 3

Thread: [EXCEL] Kill FSO or API

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Question [EXCEL] Kill FSO or API

    I want to remove all xml files in a temporary directory before proceeding with my macro.
    What is the preferred method?
    Kill is a one liner but will throw a run time error if there are no xml files in the directory.
    Code:
    Kill c:\tmp\*.xml
    FSO will require a runtime dependency (although I already have Scripting Runtime for Dictionary)
    Code:
    Public Sub DeleteFiles(strFolderName As String, strType As String)
        Dim fso As Scripting.FileSystemObject
        Dim fsoDir As Scripting.Folder
        Dim fsoFile As Scripting.File
    
        Set fso = New Scripting.FileSystemObject
        Set fsoDir = fso.GetFolder(strFolderName)
    
    
        For Each fsoFile In fsoDir.Files
            If fsoFile.Type = strType Then
                fsoFile.Delete
            End If
        Next fsoFile
    
    End Sub
    Not familiar with the API to delete all files of a certain type in a directory.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: [EXCEL] Kill FSO or API

    you can use kill, just check if file exists first
    if len(dir("c:\tmp\*.xml"))>0 then kil l"c:\tmp\*.xml"
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [EXCEL] Kill FSO or API

    Or you can use this... One situation where there is no harm in using On Error Resume Next

    vb Code:
    1. '~~> If File not found ignore error
    2. On Error Resume Next
    3.  
    4. Kill "c:\tmp\*.xml"
    5.  
    6. '~~> Immediately set it back to normal
    7. On Error GoTo 0
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width