欢迎访问yhm138的博客园博客, 你可以通过 [RSS] 的方式持续关注博客更新

MyAvatar

yhm138

HelloWorld!

使用VBScript清理%AppData%\Microsoft\InputMethod\Chs下的UDP*.tmp文件

代码

' VBScript to list UDP*.tmp files and ask user for deletion
Option Explicit

' Declare variables
Dim WSHShell, FSO, TargetFolder, FileCollection, File
Dim TargetPattern, FilesToDelete, FileCount, UserResponse

' Create objects for file and system operations
Set WSHShell = CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")

' Expand environment string to get the AppData path, then append the target folder path
TargetFolder = WSHShell.ExpandEnvironmentStrings("%AppData%") & "\Microsoft\InputMethod\Chs\"
TargetPattern = "UDP*.tmp"

' Initialize file count and file list
FileCount = 0
FilesToDelete = ""

' Check if the target folder exists
If FSO.FolderExists(TargetFolder) Then
    ' Get the collection of files in the target folder
    Set FileCollection = FSO.GetFolder(TargetFolder).Files
    ' Iterate through each file in the folder
    For Each File In FileCollection
        ' Check if the file matches the target pattern
        If FSO.GetExtensionName(File.Name) = "tmp" And Left(File.Name, 3) = "UDP" Then
            ' Keep a list of files to delete
            FilesToDelete = FilesToDelete & File.Path & vbCrLf
            ' Increment file count
            FileCount = FileCount + 1
        End If
    Next

    ' Construct the message for MsgBox
    Dim Message
    Message = FileCount & " matching file(s) found." & vbCrLf
    
    ' Display the first few file paths
    If FileCount > 0 Then
        Dim Preview, FilesArray, i
        FilesArray = Split(FilesToDelete, vbCrLf)
        Preview = "Preview of some files (maybe not all) to delete:" & vbCrLf
        For i = 0 To UBound(FilesArray)
            If Trim(FilesArray(i)) <> "" Then
                Preview = Preview & FilesArray(i) & vbCrLf
                If i >= 2 Then ' Show paths for the first 3 files (or less if fewer were found)
                    Exit For
                End If
            End If
        Next
        Message = Message & Preview & vbCrLf
    End If
    
    ' Ask the user if they want to proceed with deletion
    Message = Message & "Do you want to delete these files?"
    UserResponse = MsgBox(Message, vbYesNo + vbQuestion, "Confirm File Deletion")
    
    If UserResponse = vbYes Then
        ' User chose to delete the files
        For i = 0 To UBound(FilesArray)
            If Trim(FilesArray(i)) <> "" Then
                FSO.DeleteFile FilesArray(i), True ' Force delete
            End If
        Next
        WScript.Echo FileCount & " file(s) have been deleted."
    Else
        WScript.Echo "File deletion canceled by the user."
    End If
Else
    WScript.Echo "The target folder does not exist: " & TargetFolder
End If

' Clean up
Set WSHShell = Nothing
Set FSO = Nothing
Set FileCollection = Nothing

使用方法

代码存为clearSomeFiles.vbs
之后双击运行就行。

功能卖点:

  • 脚本会先找到所有匹配的文件,然后告诉用户找到了多少个文件。它会显示前几个文件的路径,然后提示用户是否删除这些文件。
  • 脚本使用了 MsgBox 函数来提示用户是否删除文件。这将打开一个对话框,用户可以选择“是”或“否”。如果用户选择“是”,则脚本将继续删除文件;如果用户选择“否”,脚本将输出一条消息,告知用户取消了删除操作,并且不会删除任何文件。
  • 该脚本里的删除文件操作是直接删除,不是送进回收站。

话题来源

解决 Win10 自带微软输入法卡顿

题外话——Windows操作系统下到底有多少种脚本语言?

在Windows操作系统中,有多种脚本语言可供使用。以下是一些常见的Windows脚本语言:

  1. Batch (.bat, .cmd): 这是一种很古老的脚本语言,主要用来自动化简单的命令行任务。.bat是更传统的批处理脚本扩展名,而.cmd是Windows NT系列操作系统中推荐的扩展名。

  2. VBScript (.vbs): VBScript是Visual Basic Scripting Edition的缩写,是一种由Microsoft开发的脚本语言,主要用于Windows系统的自动化任务。它可以在Windows Script Host(WSH)中运行,也可以嵌入到HTML中,被Internet Explorer浏览器执行。

  3. PowerShell (.ps1, .psm1, .psd1): PowerShell是一种跨平台(Windows、Linux和macOS)的任务自动化和配置管理框架,由Microsoft提供。它包括了一个命令行shell和脚本语言。PowerShell在Windows中已经取代了传统的命令行和VBScript作为首选自动化工具。

  4. JScript (.js): JScript是Microsoft版本的ECMAScript,通常在Windows Script Host中运行,与VBScript类似,但基于JavaScript语法。 JScript开发者: 微软。设计用途: 主要用于Windows环境中的脚本编写,可以在Internet Explorer浏览器中运行,也可以在Windows Script Host(WSH)中使用。兼容性: 由于JScript是微软的实现,它包含了一些专门为Windows平台设计的扩展,并且最佳的兼容性是在IE浏览器和Windows系统中。JavaScript开发者: 最初由Netscape开发,现在由ECMA国际组织标准化。设计用途: 作为Web开发的一部分,用于添加交互性和动态功能到网页中。它是所有现代Web浏览器的标准脚本语言。兼容性: 作为一种标准化的语言,JavaScript在所有主流浏览器(如Chrome, Firefox, Safari等)中运行,不限于特定平台。

  5. Windows Management Instrumentation (WMI): 虽然WMI本身不是一种脚本语言,但它提供了一组扩展的Windows管理接口,可以通过脚本语言(如VBScript和PowerShell)来调用WMI接口来访问和管理Windows系统。

  6. Python、Perl、Tcl、Lua脚本之类的就不提了

posted @ 2024-01-04 17:02  yhm138  阅读(34)  评论(0编辑  收藏  举报