Delete Files by extension

Author Message
rastalin

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 3/20/2008
  • Status: offline
Delete Files by extension Friday, September 19, 2008 9:35 PM (permalink)
0
So I have some content servers that require (when we push some types of code) a full flush of 10's of thousands of files that reside on said servers. I did some research on the net hoping to find that someone else had been though the pain I just did, to no avail. I found some that were similar to what I needed but I had to add more and make some changes. The only ones that I found were extension only, but my need was for a wildcard in the middle of the string and to ID all files that I wanted to delete and to only delete the files that were a said ext. So as you can see from the script I needed ‘data’ & wildcard & “.” & ext.
 
I figured that I had to do it, I might as well help someone else out with my pain. Please feel free to let me know if there is an easier way to do this, or a better one for that matter.




 
 ' ====================================================================================
 ' TITLE:                    delete_files_by_extension.vbs
 ' AUTHOR:               ME
 ' HOW TO USE:        Run it from computer/server that has access to content servers.
 ' ====================================================================================
 
 OPTION EXPLICIT
 DIM strExtensionsToDelete,strFolder
 DIM objFSO
 
 ' ====================================================================================
 ' TITLE:            Input and MsgBox
 ' PURPOSE:            Runs and input data.
 ' ====================================================================================
 
 DIM Title, Text1
 DIM Input, result
 
 Title = "File removal script"                                                                            'Script Title
 
 Input = inputbox("Enter Directory To Scan", Title)                                        'InputBox Tree
    If Input = "" then 
       objExplorer.Document.Body.InnerHTML = "You must enter a valid directory"
       wscript.quit
      else
    end if 
    
 Text1 = "You selected " & Input & " is this correct?"                                  'Misc Text - Edit as needed
 
 result= MsgBox(Text1, vbInformation + vbYesNo, Title)                          'Results data
 If result =vbYes Then                                                                                 'Result = Yes
       MsgBox ("Application Started, it will finish soon.")                                'MsgBox on vbYes
 Else
       MsgBox "You fail, Program Terminated"                                                'MsgBox on vbNo
 End if
 
 
 strFolder = Input
 strExtensionsToDelete = "html"                                                                                        'This is where you add your file extensions, you can add more by adding ',' e.g 'html, mp3, rar, mov' etc.
 set objFSO = createobject("Scripting.FileSystemObject")
 RecursiveDeleteByExtension strFolder,strExtensionsToDelete
 
 ' ====================================================================================
 ' TITLE:               Sub
 ' PURPOSE:          Run and choose a folder to analyze.
 ' ====================================================================================
 
 sub RecursiveDeleteByExtension(byval strDirectory,strExtensionsToDelete)
     DIM objFolder, objSubFolder, objFile
     DIM strExt
     DIM strFileName
     
     set objFolder = objFSO.GetFolder(strDirectory)
     for each objFile in objFolder.Files
         for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")                    
             strFileName = objFSO.GetFileName(objFile.Path)            
                 IF RIGHT(UCASE(strFileName),LEN(strExt)+1) = "." & strExt then                'This is where you tell it to grab the extension
                     IF LEN(strFileName) >= LEN(strExt)+15 THEN                                'Change the LEN to whatever number you need
                         IF LEFT(UCASE(strFileName), 14) = "15_CHARS_WORTH_OF_DATA" THEN        'See line above
                             objFile.Delete
                             exit for   
                         END IF
                     END IF
                 END IF
             next
         next    
     for each objSubFolder in objFolder.SubFolders                                            'This is the call to do the same in Sub Folders
         RecursiveDeleteByExtension objSubFolder.Path,strExtensionsToDelete                    'Recursive
     next
 end sub

 
 
#1
    mgonzalez

    • Total Posts : 1
    • Scores: 0
    • Reward points : 0
    • Joined: 5/20/2009
    • Status: offline
    RE: Delete Files by extension Wednesday, May 20, 2009 1:25 AM (permalink)
    0
    Hello
     
    Thanks a lot for your code.
    I can't seem to make it work though. It doesn't give me any error at all, but it can't delete the files. Any idea?
     
    Thanks
     
    Marcela
     
    #2
      nate.person

      • Total Posts : 14
      • Scores: 0
      • Reward points : 0
      • Joined: 5/21/2009
      • Status: offline
      RE: Delete Files by extension Wednesday, June 03, 2009 2:07 PM (permalink)
      0
      what about os difference? (xp vs. vista)
       
      #3
        rastalin

        • Total Posts : 2
        • Scores: 0
        • Reward points : 0
        • Joined: 3/20/2008
        • Status: offline
        RE: Delete Files by extension Tuesday, August 18, 2009 5:33 AM (permalink)
        0
        nate.person


        what about os difference? (xp vs. vista)


        With each OS there shouldn't be much to change.

        As for the lack of error your getting ~ remember we are searching for EXACTLY 15 char's here. You can read the comments I posted to change as needed.

        Let me know if you need some help, and it would might be a good ideal to post what your attempting to do so that I can walk you though it a bit.
         
        #4

          Online Bookmarks Sharing: Share/Bookmark

          Jump to:

          Current active users

          There are 0 members and 1 guests.

          Icon Legend and Permission

          • New Messages
          • No New Messages
          • Hot Topic w/ New Messages
          • Hot Topic w/o New Messages
          • Locked w/ New Messages
          • Locked w/o New Messages
          • Read Message
          • Post New Thread
          • Reply to message
          • Post New Poll
          • Submit Vote
          • Post reward post
          • Delete my own posts
          • Delete my own threads
          • Rate post

          2000-2012 ASPPlayground.NET Forum Version 3.9