如何让 vbs 文件删除其他文件

如何让 vbs 文件删除其他文件

我有一个垃圾文件夹,里面存放着我下载的东西。有时,我想通过删除特定扩展名的某些文件来清理该文件夹。我该怎么做呢?

答案1

干得好

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

如果“C:\JunkFolder”是正确的道路,

那么它将是:

target_folder = "C:\JunkFolder"
Set fs = CreateObject("Scripting.Filesystemobject")
fs.DeleteFile( target_folder & "\*.txt" )

将上述代码粘贴到扩展名为 .vbs 的文件中。

如果你点击 vbs 文件,则“C:\JunkFolder”中的每个 .txt 文件都将被删除

“这可以通过 .hta 文件来实现,这样你就可以选择你想要定位的文件夹。”

对于那些想要使用此脚本删除所有 .txt 文件的人来说

例如,在 Program Files 文件夹中,但您启用了用户帐户控制 (UAC),

使用下面的脚本来获得 UAC 提示,因为如果没有它,脚本将不会执行任何操作。

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

答案2

瞧!

我写了一个简单的 hta 来浏览文件夹,

您想要删除所有具有特定扩展名的文件。

您可以在 hta 的文本字段中输入扩展名,

然后单击浏览按钮导航到所需的文件夹。

将完整代码粘贴到文本文件中,

然后将文本文件的扩展名更改为 .hta

如果您看不到文件扩展名,

您必须取消选中隐藏已知文件类型的扩展名在文件夹选项中。

<html>
<head>

<script Language="JScript">
window.resizeTo(0,0);
window.moveTo(-1500,-1500);
</script>

<hta:application
Id="oHTA"
applicationName="&nbsp;&nbsp;&nbsp;Remove all files with specific extension from a folder&nbsp;&nbsp;&nbsp;"
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 Id="MMSize" 
    classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
    <param name="command" value="minimize">
</object>

</head>
<body onload="setTimeout('position(200,200)',0)"
        style="border:4px solid #AA000A;">

<textarea
disabled
Style="position:absolute;
border:0px solid transparent;
filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr='#CECECE',endColorStr='#FA9100');
top:0px;
left:0px;
width:200px;
height:200px;
overflow-x:hidden;
overflow-y:hidden;">
</textarea>

<input type="text"
    class="extensionInput"
    name="userExtensionInput"
    wrap="off"  
    Style="position:absolute;
    border:3px solid #AA000A;
    top:150px;
    left:67px;
    width:74px;
    height:26px;
    font-family:arial;
    font-weight:bold;
    text-align:center;
    overflow-x:hidden;
    overflow-y:hidden;"
    title="&nbsp;&nbsp;&nbsp;Enter the extension of the files you want to delete&nbsp;&nbsp;"/>

<input type="button"
    class="folder_choice"
    onmouseover="this.style.color='#004E98'"
    onmouseout="this.style.color='#000000'"
    Style="position:absolute;
    border:0px solid transparent;
    top:110px;
    left:67px;
    width:74px;
    height:26px;
    text-align:center;
    font-family:arial;
    font-weight:bold;
    font-size:14px;"
    value="Browse"
    onClick="choose_folder()"
    title="&nbsp;&nbsp;&nbsp;Browse for folder&nbsp;&nbsp;"/>

<input type="button"
    class="closeButton"
    onmouseover="this.style.color='#FF0000'"
    onmouseout="this.style.color='#000000'"
    style="position:absolute;
    border:0px solid transparent;
    top:21px;
    left:141px;
    width:46px;
    height:26px;
    text-align:center;
    font-family:arial;
    font-weight:bold;
    font-size:14px;"
    value="EXIT"
    onKeyDown="exitOnKeyDown()"
    onClick="window.close()"
    title="&nbsp;&nbsp;&nbsp;Close&nbsp;&nbsp;"/>

<input type="button"
    class="minimizeButton"
    onmouseover="this.style.color='#7F7F00'"
    onmouseout="this.style.color='#000000'"
    style="position:absolute;
    border:0px solid transparent;
    top:21px;
    left:21px;
    width:46px;
    height:26px;
    text-align:center;
    font-family:arial;
    font-weight:bold;
    font-Size:14px;"
    value="MIN" 
    onClick="MMSize.Click()" 
    title="&nbsp;&nbsp;&nbsp;&nbsp;Minimize window&nbsp;&nbsp;&nbsp;"/>

<font color="#000000"
    style="position:absolute;
    top:70;
    left:68;
    font-family:verdana;
    font-weight:bold;
    color:#2E3150;"
    title="&#13;&thinsp;&thinsp;&thinsp;&thinsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dr RedAnt&nbsp;&nbsp;&#13;&thinsp;&thinsp;&thinsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SweetSoft&#13;&nbsp;&nbsp;&nbsp;Utopia-Gateway&nbsp;&thinsp;&#13;&nbsp;">
    <font Size="2px">Dr RedAnt</font></font>

</body>
</html>

HTA 的样子如下

从文件夹中删除所有具有特定扩展名的文件

如果你想以简单的方式获取文件,那么这里有一个下载链接

相关内容