none
VB script to delete files of a certain extension RRS feed

  • Question

  • Hi,

    I am using below VB script to delete files from folder, but it asks me to click OK before deleting each file. Please let me know how to avoid that

    OPTION EXPLICIT
    DIM strExtensionsToDelete,strFolder
    DIM objFSO, MaxAge, IncludeSubFolders

    ' ************************************************************
    ' Setup
    ' ************************************************************

    ' Folder to delete files
    strFolder = "D:\test"
    ' Delete files from sub-folders?
    includeSubfolders = true
    ' A comma separated list of file extensions
    ' Files with extensions provided in the list below will be deleted
    strExtensionsToDelete = "log"
    ' Max File Age (in Days).  Files older than this will be deleted.
    maxAge = 10

    ' ************************************************************

    set objFSO = createobject("Scripting.FileSystemObject")

    DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders

    wscript.echo "Finished"

    sub DeleteFiles(byval strDirectory,byval strExtensionsToDelete,byval maxAge,includeSubFolders)
        DIM objFolder, objSubFolder, objFile
        DIM strExt

        set objFolder = objFSO.GetFolder(strDirectory)
        for each objFile in objFolder.Files
            for each strExt in SPLIT(UCASE(strExtensionsToDelete),",")
                if RIGHT(UCASE(objFile.Path),LEN(strExt)+1) = "." & strExt then
                    IF objFile.DateLastModified < (Now - MaxAge) THEN
                        wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified
                        objFile.Delete
                        exit for
                    END IF
                end if
            next
        next    
        if includeSubFolders = true then ' Recursive delete
            for each objSubFolder in objFolder.SubFolders
                DeleteFiles objSubFolder.Path,strExtensionsToDelete,maxAge, includeSubFolders
            next
        end if
    end sub


    Alex

    Thursday, September 27, 2012 3:24 PM

Answers

  • Either remove the "wscript.echo" line right before the "objFile.delete" line, if you don't need to see a confirmation of which file is being deleted.  Or execute the script from a command prompt console window with the cscript.exe host, something like this ...

     cscript pathspec\yourscriptname.vbs

    The file names being deleted will output to the console window, but no confirmation will be required.  To keep a log of which files were deleted, you can redirect that output to a log file ...

     cscript pathspec\yourscriptname.vbs > someplace\yourlog.txt


    Tom Lavedas

    • Marked as answer by Alex Rabbi Thursday, September 27, 2012 4:32 PM
    Thursday, September 27, 2012 3:41 PM
    Moderator

All replies

  • Alex - that script does not ask for anything.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, September 27, 2012 3:40 PM
    Thursday, September 27, 2012 3:39 PM
  • Either remove the "wscript.echo" line right before the "objFile.delete" line, if you don't need to see a confirmation of which file is being deleted.  Or execute the script from a command prompt console window with the cscript.exe host, something like this ...

     cscript pathspec\yourscriptname.vbs

    The file names being deleted will output to the console window, but no confirmation will be required.  To keep a log of which files were deleted, you can redirect that output to a log file ...

     cscript pathspec\yourscriptname.vbs > someplace\yourlog.txt


    Tom Lavedas

    • Marked as answer by Alex Rabbi Thursday, September 27, 2012 4:32 PM
    Thursday, September 27, 2012 3:41 PM
    Moderator
  • Thanks very much Tom ..it worked

    I removed "wscript.echo" line


    Alex

    Thursday, September 27, 2012 4:34 PM
  • Alex - that script does not ask for anything.


    ¯\_(ツ)_/¯

    Maybe it won't "ask" but it will certainly pause when executed with wscript.exe:

    wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified

    Thursday, September 27, 2012 4:46 PM
  • Alex - that script does not ask for anything.


    ¯\_(ツ)_/¯

    Maybe it won't "ask" but it will certainly pause when executed with wscript.exe:

    wscript.echo "Deleting:" & objFile.Path & " | " & objFile.DateLastModified

    Saying that is asks for approval was misleading.  I assumed the person asking the question had written the scritp and knew what it was doing.  I tend to not notice when the question is from someone who is not a WIndows techniician or who has no experience with scripting.  I should be more alert to that particular cause of the message box.  On all system I work on 'cscript' is the defult by policy setting.

    Tom picked it up right away so all is well.


    ¯\_(ツ)_/¯

    Thursday, September 27, 2012 5:09 PM
  • righ now i used batch file on startup windows xp, but i want to use vbscript because the cmd will be disable on plan. my batch script like this.

    Respondents in this forum will gladly help posters with their script problems but they do not write scripts on demand. Here are a couple of options for you:

    • Learn how to write VBScripts or PowerShell.
    • Run the job as a scheduled task at boot time under an account other than the logon account.
    Tuesday, May 13, 2014 6:13 AM
  • how to change path destination "D:\test" direct to a drive, like "D:" and work on all of its folders an sub folders..

    So, delete files based on some extensions on a drive and all of its folders an subfolders.

    Wednesday, May 14, 2014 3:45 AM