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

发布时间 2024-01-04 17:06:35作者: yhm138

代码

' 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

    ' Inform user of the number of files found
    WScript.Echo FileCount & " matching file(s) found."
    
    ' Display the first few file paths
    If FileCount > 0 Then
        Dim Preview, FilesArray, i
        FilesArray = Split(FilesToDelete, vbCrLf)
        Preview = "Preview of files 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
        WScript.Echo Preview
        
        ' Ask the user if they want to proceed with deletion
        UserResponse = MsgBox("Do you want to delete these files?", 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
    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 自带微软输入法卡顿