Results 1 to 9 of 9

Thread: Delete a file with a given extension

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Delete a file with a given extension

    Hi All,

    Wondering if anyone can help, I'm attempting to delete a .ost file from a users roaming profile, I'm using the following code:

    Dim strComputer, oWSH, oWMI, oFSO, AppDataFolder

    strComputer = "."
    Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set oWSH = CreateObject("WScript.Shell")
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    AppDataFolder = oWSH.ExpandEnvironmentStrings("%appdata%")

    For Each file As String In IO.Directory.GetFiles(AppDataFolder & "\Thinstall\Microsoft Standard 2010 KMS\%Local AppData%\Microsoft\Outlook","*.ost")
    IO.File.Delete(file)
    Next


    I keep getting "Expected In" error message on line 9 char 15. Any idea why? Sorry but I literally do no VB coding so am a bit lost.

    Regards,
    Ross

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Delete a file with a given extension

    Here is one way to do what you want:
    Code:
            Dim desired_ext As String = ".ost"
            Dim target_directory As New IO.DirectoryInfo("C:\Users\pc1\AppData\Roaming") 'My %appdata% folder
            Dim get_files As IO.FileInfo() = target_directory.GetFiles()
    
            'Loop through each file in our array
            For Each file As IO.FileInfo In get_files
                'Conditional statement to check if our file has the .ost
                If IO.Path.GetExtension(file.ToString) = desired_ext Then
                    'Bye bye
                    file.Delete()
                End If
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: Delete a file with a given extension

    Quote Originally Posted by dday9 View Post
    Here is one way to do what you want:
    Code:
            Dim desired_ext As String = ".ost"
            Dim target_directory As New IO.DirectoryInfo("C:\Users\pc1\AppData\Roaming") 'My %appdata% folder
            Dim get_files As IO.FileInfo() = target_directory.GetFiles()
    
            'Loop through each file in our array
            For Each file As IO.FileInfo In get_files
                'Conditional statement to check if our file has the .ost
                If IO.Path.GetExtension(file.ToString) = desired_ext Then
                    'Bye bye
                    file.Delete()
                End If
            Next
    Hi dday9,

    Thanks a bunch for the reply but I'm still getting errors, it doesn't appear to like the As String part on line one, this is the exact problem I was having with my example, it's saying it expects end of statement, any idea why? Here is what I done below which is pretty much a copy of what you have provided:

    Dim desired_ext As String = ".ost"
    Dim target_directory As New IO.DirectoryInfo("V:\Users\rod\AppData\Roaming\Thinstall\Microsoft Standard 2010 KMS\%Local AppData%\Microsoft")
    Dim get_files As IO.FileInfo() = target_directory.GetFiles()

    For Each file As IO.FileInfo In get_files
    If IO.Path.GetExtension(file.ToString) = desired_ext Then
    file.Delete()
    End If
    Next


    Thanks,
    Ross

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Delete a file with a given extension

    Post a picture, if you would, of a screen shot of the errors. Because I just tried the example in post #2 and it executed without errors.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: Delete a file with a given extension

    Name:  screen.png
Views: 549
Size:  40.1 KB

    Please see attached image, very strange the error, I have other VB scripts which are running fine, I am executing them on Windows 7 if that matters.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Delete a file with a given extension

    Ahh, ok. This is not visual basic.net, that would've been nice to know. Let me message a mod for you.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    4

    Re: Delete a file with a given extension

    Sorry my mistake, doh, if a mod could please transfer this to the correct forum it would be appreciated.

    Sorry for wasting your time.

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Delete a file with a given extension

    Moved to the VBScript forum.

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Delete a file with a given extension

    You're mixing VBScript and VB.Net code. Remove the For Each loop and replace it with code similar to this:
    Code:
    oFSO.DeleteFile AppDataFolder & "\*.ost", True
    When the second argument is set to True the FileSystemObject will try to delete the file even if the read-only attribute is set on the file. Note that this does not do a recursive search through any sub directories.

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