本帖最后由 doswork 于 2016-8-6 21:38 编辑
yu2n 发表于 2016-7-2 01:46
您好“yu2n”老师,有个问题还得请教您:
我想实现的功能:通过运行“c:\test\del.vbs” 彻底删除“c:\test”目录及子目录所有文件,无论是否有“只读/隐藏”属性的文件/文件夹
找到一个代码,可以实现上面的功能,但是只能在XP系统下使用有效,结合您给的“以管理员权限运行”代码,组合之后在win7以上系统运行无效,帮忙看下是哪里的问题,多谢!
组合之后的代码如下:- Option Explicit
-
- RunAsAdmin
-
- 'Msgbox CreateObject("WScript.Shell").CurrentDirectory
-
-
- ' 以管理员身份运行 By Yu2n
- Sub RunAsAdmin()
- Dim oItems, vItem, sVer, nVer, vArg, sArgs, sCurDir
- Set oItems = GetObject("winmgmts:").InstancesOf("Win32_OperatingSystem")
- For Each vItem In oItems
- sVer = vItem.Version
- Next
- Set oItems = Nothing
- nVer = Clng(Split(sVer, ".")(0) & Split(sVer, ".")(1))
- If nVer >= 60 Then
- If Not WScript.Arguments.Named.Exists("RunAsAdmin") Then
- For Each vArg In WScript.Arguments
- sArgs = sArgs & " """ & vArg & """"
- Next
- sArgs = sArgs & " /RunAsAdmin:True"
- CreateObject("Shell.Application").ShellExecute "WScript.exe", _
- """" & WScript.ScriptFullName & """" & sArgs, "", "runas", 1
- WScript.Quit(0)
- Else
- sCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\") -1)
- CreateObject("WScript.Shell").CurrentDirectory = sCurDir
- End If
- End If
- End Sub
-
-
-
-
- dim fso,VBSFolder,tempbat,cbat,obat
-
- Set fso = CreateObject("Scripting.FileSystemObject")
- VBSFolder = fso.GetFile(Wscript.ScriptFullName).ParentFolder.Path
- tempbat = fso.GetSpecialFolder(2)+"\tem.bat"
- set Cbat=fso.createtextfile(tempbat,true)
- Cbat.writeline "pushd "+chr(34)+fso.GetSpecialFolder(2)+chr(34)
- Cbat.close
- set Obat = fso.opentextfile(tempbat,8)
- Obat.writeline "timeout /T 1 /NOBREAK"
- Obat.writeline "rd /s /q "+chr(34)+VBSFolder+chr(34)
- Obat.writeline "del /F /Q %0"
- Obat.close
- createobject("wscript.shell").run chr(34)+tempbat+chr(34),0,false
- WScript.Quit
复制代码
|