Tag Archives

batch file

Windows Batch File to Remove Junk Files (.png, .txt, .srt, .nfo)

May 12, 2017 0 comments

Since I download a ton of different files I’m always left with a hard drive cluttered with a mess of junk files.

So I wrote up a Windows bat file that will scan a directory on my computer and remove the following junk files:

  • txt
  • jpg
  • nfo
  • png
  • gif
  • bmp
  • srt

You can easily add your own file extensions to remove.  This will also check inside of folders as well.

Here is the complete batch file code, please note you can also download this below.

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

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

' Folder to delete files
strFolder = "I:###Downloads"
' 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 = "txt,jpg,nfo,png,gif,bmp,srt"
' Max File Age (in Days).  Files older than this will be deleted.
maxAge = 0

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

set objFSO = createobject("Scripting.FileSystemObject")

DeleteFiles strFolder,strExtensionsToDelete, maxAge, includeSubFolders



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
                    
                    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

Remove Junk Files Batch (73 downloads)