I have a junk folder that I download stuff to, and every once in a while, I want to clean up the folder by deleting certain files of a certain extension. How would I be able to do that?
32 Answers
Here you go
target_folder = "INSERT HERE FULL PATH OF FOLDER FROM WHICH YOU WANT TO DELETE ALL .TXT FILES"
Set fs = CreateObject("Scripting.Filesystemobject")
fs.DeleteFile( target_folder & "\*.txt" )If "C:\JunkFolder" is the right path,
then it would be :
target_folder = "C:\JunkFolder"
Set fs = CreateObject("Scripting.Filesystemobject")
fs.DeleteFile( target_folder & "\*.txt" )Paste the above code in a file with the extension .vbs.
If you click the vbs file, every .txt file will be deleted in "C:\JunkFolder"
"This could be made with a .hta file so you could choose the folder you want to target."
For those people who want to use this script for deleting all the .txt
in for example a folder in Program Files, but you have User Account Control (UAC) enabled,
use the below script to have a UAC prompt, because without it the script wouldn't do anything.
If WScript.Arguments.length = 0 Then
Set sa = CreateObject("Shell.Application")
sa.ShellExecute "wscript.exe", Chr(34) & _
WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else
target_folder = "PATH BETWEEN THESE QUOTES" Rem <-- Insert the path of the folder you want to target
Set fs = CreateObject("Scripting.Filesystemobject")
fs.DeleteFile( target_folder & "\*.txt" ) Rem <-- Change the extension txt to another, like pdf, exe, png...
End If 1 Voila
I've wrote a simple hta to browse for the folder,
in which you want to remove all the files with a specific extension.
You can enter an extension in the textfield of the hta,
and then click the Browse button to navigate to the desired folder.
Paste the complete code in a text file,
and then change the extension of the text file to .hta
If you cannot see the file extension,
you have to uncheck the checkbox for Hide extensions for known file types in folder options.
<html>
<head>
<script Language="JScript">
window.resizeTo(0,0);
window.moveTo(-1500,-1500);
</script>
<hta:application
applicationName=" Remove all files with specific extension from a folder "
border="thin"
borderStyle="no"
caption="no"
contextMenu="yes"
icon="" /** <-- Insert the path of your icon between the quotes */
innerBorder="no"
minimizeButton="no"
maximizeButton="no"
navigable="no"
scroll="no"
scrollFlat="no"
selection="no"
singleInstance="yes"
showInTaskbar="yes"
sysMenu="no"
windowState="normal" />
<script language="JScript">
function position(iWidth,iHeight){
var iLeft = (window.screen.width-iWidth)/2;
var iTop = (window.screen.height-iHeight)/3;
window.moveTo(iLeft,iTop);
window.resizeTo(iWidth,iHeight);
document.title = oHTA.applicationName;}
</script>
<style type="text/css">
.minimizeButton {Filter:progid:DXImageTransform.Microsoft.Gradient (GradientType=0,StartColorStr='#F9F99F',endColorStr='#7F7920');}
.closeButton {Filter:progid:DXImageTransform.Microsoft.Gradient (GradientType=0,StartColorStr='#F9F99F',endColorStr='#FF3920');}
.folder_choice {Filter:progid:DXImageTransform.Microsoft.Gradient (GradientType=0,StartColorStr='#F9F99F',endColorStr='#FF0000');}
.extensionInput {Filter:progid:DXImageTransform.Microsoft.Gradient (GradientType=0,StartColorStr='#F9F99F',endColorStr='#FF0000');}
</style>
<script language="VBScript">
Sub exitOnKeyDown() If Window.Event.KeyCode = 13 Then window.close() End If
End Sub
Sub choose_folder()
On Error Resume Next userExtensionChoice = userExtensionInput.value If userExtensionChoice = "" Then MsgBox "You forgot to enter an extension in the textfield", 4096 + 48,"NO EXTENSION" Location.Reload Exit Sub End If
Set sa = CreateObject("Shell.Application")
Set bf = sa.BrowseForFolder( 0, "Select the target folder...", 1, "" ) REM Between last quotes you can enter a path which will default open when you start to browse If bf Is Nothing Then Exit Sub Else Set fs = CreateObject("Scripting.Filesystemobject") If MsgBox("You selected the folder : " & vbCrLf & vbCrLf & bf.Self.path & _ vbCrLf & vbCrLf & "You chose the file extension '" & userExtensionChoice & "'" & _ vbCrLf & vbCrLf & "Click OK if you want to proceed," & _ vbCrLf & "or Cancel to abort. ", 4096 + 1 + 64," Check") = 1 Then fs.DeleteFile bf.Self.path & "\*." & userExtensionChoice Else Exit Sub End If End If
End Sub
</script>
<object classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11"> <param name="command" value="minimize">
</object>
</head>
<body onload="setTimeout('position(200,200)',0)">
<textarea
disabled>
</textarea>
<input type="text" name="userExtensionInput" wrap="off" title=" Enter the extension of the files you want to delete "/>
<input type="button" onmouseover="this.style.color='#004E98'" onmouseout="this.style.color='#000000'" value="Browse" onClick="choose_folder()" title=" Browse for folder "/>
<input type="button" onmouseover="this.style.color='#FF0000'" onmouseout="this.style.color='#000000'" value="EXIT" onKeyDown="exitOnKeyDown()" onClick="window.close()" title=" Close "/>
<input type="button" onmouseover="this.style.color='#7F7F00'" onmouseout="this.style.color='#000000'" value="MIN" onClick="MMSize.Click()" title=" Minimize window "/>
<font color="#000000" title="      Dr RedAnt     SweetSoft Utopia-Gateway   "> <font Size="2px">Dr RedAnt</font></font>
</body>
</html>This is how the HTA looks
If you want the file the easy way, then here is a download link