[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
  1. Option Explicit
  2. RunAsAdmin
  3. Msgbox CreateObject("WScript.Shell").CurrentDirectory
  4. ' 以管理员身份运行 By Yu2n
  5. Sub RunAsAdmin()
  6. Dim oItems, vItem, sVer, nVer, vArg, sArgs, sCurDir
  7. Set oItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  8. For Each vItem In oItems
  9. sVer = vItem.Version
  10. Next
  11. Set oItems = Nothing
  12. nVer = Clng(Split(sVer, ".")(0) & Split(sVer, ".")(1))
  13. If nVer >= 60 Then
  14. If Not WScript.Arguments.Named.Exists("RunAsAdmin") Then
  15. For Each vArg In WScript.Arguments
  16. sArgs = sArgs & " """ & vArg & """"
  17. Next
  18. sArgs = sArgs & " /RunAsAdmin:True"
  19. CreateObject("Shell.Application").ShellExecute "WScript.exe", _
  20. """" & WScript.ScriptFullName & """" & sArgs, "", "runas", 1
  21. WScript.Quit(0)
  22. Else
  23. sCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\") -1)
  24. CreateObject("WScript.Shell").CurrentDirectory = sCurDir
  25. End If
  26. End If
  27. End Sub
复制代码
1

评分人数

    • doswork: 新手分少,少给点儿,见谅~ 多谢!技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

本帖最后由 yu2n 于 2016-8-6 23:32 编辑

回复 5# doswork
  1. Option Explicit
  2. '以管理员身份运行
  3. RunAsAdmin
  4. '待删除文件夹
  5. Const DEL_DIR = "C:\TEST"
  6. Dim wso, cmdLine
  7. Set wso = CreateObject("WScript.Shell")
  8. '命令:移除属性,删除文件夹/子文件/子文件夹
  9. cmdLine = "cmd /c " & _
  10. "attrib -R -A -S -H /S /D """ & DEL_DIR & """ & " & _
  11. "if exist """ & DEL_DIR & """ rd /s /q """ & DEL_DIR & """"
  12. ' 等于执行CMD命令:
  13. ' attrib -R -A -S -H /S /D "C:\test"
  14. ' if exist "C:\test" rd /s /q "C:\test"
  15. '执行命令,回报结果
  16. If wso.Run(cmdLine, 0, True) = 0 Then
  17. Msgbox DEL_DIR & " 删除成功!", vbInformation, WScript.ScriptFullName
  18. Else
  19. Msgbox DEL_DIR & " 删除失败!", vbCritical, WScript.ScriptFullName
  20. End If
  21. ' 以管理员身份运行 By Yu2n
  22. Sub RunAsAdmin()
  23. Dim oItems, vItem, sVer, nVer, vArg, sArgs, sCurDir
  24. Set oItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  25. For Each vItem In oItems
  26. sVer = vItem.Version
  27. Next
  28. Set oItems = Nothing
  29. nVer = Clng(Split(sVer, ".")(0) & Split(sVer, ".")(1))
  30. If nVer >= 60 Then
  31. If Not WScript.Arguments.Named.Exists("RunAsAdmin") Then
  32. For Each vArg In WScript.Arguments
  33. sArgs = sArgs & " """ & vArg & """"
  34. Next
  35. sArgs = sArgs & " /RunAsAdmin:True"
  36. CreateObject("Shell.Application").ShellExecute "WScript.exe", _
  37. """" & WScript.ScriptFullName & """" & sArgs, "", "runas", 1
  38. WScript.Quit(0)
  39. Else
  40. sCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\") -1)
  41. CreateObject("WScript.Shell").CurrentDirectory = sCurDir
  42. End If
  43. End If
  44. End Sub
复制代码
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

  1. Option Explicit
  2. Main
  3. Sub Main()
  4. '以管理员身份运行
  5. RunAsAdmin
  6. '待删除文件夹
  7. Const DEL_DIR = "C:\TEST"
  8. Dim wso, cmdLine
  9. Set wso = CreateObject("WScript.Shell")
  10. '命令:移除属性,删除文件夹/子文件/子文件夹
  11. cmdLine = "cmd /c " & _
  12. "attrib -R -A -S -H /S /D """ & DEL_DIR & """ & " & _
  13. "if exist """ & DEL_DIR & """ rd /s /q """ & DEL_DIR & """"
  14. Call wso.Run(cmdLine, 0, True)
  15. '命令:创建文件夹
  16. cmdLine = "cmd /c " & _
  17. "if not exist """ & DEL_DIR & """ md """ & DEL_DIR & """"
  18. Call wso.Run(cmdLine, 0, True)
  19. '提示完成
  20. Msgbox "完成!", vbInformation, WScript.ScriptFullName
  21. End Sub
  22. ' 以管理员身份运行 By Yu2n
  23. Sub RunAsAdmin()
  24. Dim oItems, vItem, sVer, nVer, vArg, sArgs, sCurDir
  25. Set oItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
  26. For Each vItem In oItems
  27. sVer = vItem.Version
  28. Next
  29. Set oItems = Nothing
  30. nVer = Clng(Split(sVer, ".")(0) & Split(sVer, ".")(1))
  31. If nVer >= 60 Then
  32. If Not WScript.Arguments.Named.Exists("RunAsAdmin") Then
  33. For Each vArg In WScript.Arguments
  34. sArgs = sArgs & " """ & vArg & """"
  35. Next
  36. sArgs = sArgs & " /RunAsAdmin:True"
  37. CreateObject("Shell.Application").ShellExecute "WScript.exe", _
  38. """" & WScript.ScriptFullName & """" & sArgs, "", "runas", 1
  39. WScript.Quit(0)
  40. Else
  41. sCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\") -1)
  42. CreateObject("WScript.Shell").CurrentDirectory = sCurDir
  43. End If
  44. End If
  45. End Sub
复制代码
注意:无法删除使用中的文件/文件夹。
1

评分人数

    • doswork: 多谢帮助新手~技术 + 1
『千江有水千江月』千江有水,月映千江;万里无云,万里青天。    http://yu2n.qiniudn.com/

TOP

返回列表