如何使用 VBScript 关闭特定文件夹? [英] How to close a specific folder with VBScript?

查看:40
本文介绍了如何使用 VBScript 关闭特定文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VBScript 制作一个简单的程序,该程序每次打开时都会关闭特定文件夹,从而拒绝访问该文件夹.我已经成功地将这段代码用于许多文件夹,但由于某种原因它不适用于 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.

I am trying to make a simple program with VBScript that closes a specific folder every time it gets opened, thus denying access to that folder. I've successfully used this code right here for many folders, but for some reason it doesn't work with C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp.

Do
    WindowTitle = "FOLDERNAME"
    Set shell = CreateObject("WScript.Shell") 
    success = shell.AppActivate(WindowTitle)
    If success Then shell.SendKeys "%{F4}" 
Loop

有什么办法可以拒绝使用 .vbs 文件访问该特定文件夹吗?

Is there any way that I can deny access to that specific folder using .vbs files?

推荐答案

你可以这样试试:

myfolder = "C:\temp"
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
    If w.document.folder.self.Path = myfolder Then w.Quit
Next

这是关闭临时文件夹的完整示例:

And here is a complete example to close your temporary folder :

Option Explicit
If AppPrevInstance() Then 
    MsgBox "There is an existing proceeding !" & VbCrLF &_
    CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"    
    WScript.Quit   
Else
Dim MyFolder,ws
Set ws = CreateObject("wscript.shell")
Myfolder = ws.ExpandEnvironmentStrings("%temp%")
Do
    Call CloseThis(MyFolder)
    wscript.sleep 1000
Loop
End If
'*********************************************************************************************
Sub CloseThis(Folder)
Dim sh,w
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
    If w.document.folder.self.Path = Folder Then w.Quit
Next
End Sub
'*********************************************************************************************
Function AppPrevInstance()   
    With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")   
        With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE " & CommandLineLike(WScript.ScriptFullName) & _
            " AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")   
            AppPrevInstance = (.Count > 1)   
        End With   
    End With   
End Function    
'*********************************************************************************************
Function CommandLineLike(ProcessPath)   
    ProcessPath = Replace(ProcessPath, "\", "\\")   
    CommandLineLike = "'%" & ProcessPath & "%'"   
End Function
'*********************************************************************************************

这篇关于如何使用 VBScript 关闭特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆