1.Roaming
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%APPDATA%")
wscript.echo strHomeFolder
//C:\Users\username\AppData\Roaming
2.Local
Const ssfAPPDATA = &H1C
Set oShell = CreateObject("Shell.Application")
strHomeFolder = oShell.NameSpace(ssfAPPDATA).Self.Path
wscript.echo strHomeFolder
//C:\Users\username\AppData\Local
3.拼接
Const ssfAPPDATA = &H1C
Set oShell = CreateObject("Shell.Application")
strHomeFolder = oShell.NameSpace(ssfAPPDATA).Self.Path
strAppDataPath = strHomeFolder & "\cc"
Wscript.Echo strAppDataPath
4.获取当前脚本路径
strPath = Wscript.ScriptFullName
Wscript.Echo strAppDataPath
5.获取脚本当前目录
strPath = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder
Wscript.Echo strPath
6.获取脚本当前目录的父目录
strPath = createobject("Scripting.FileSystemObject").GetFile(Wscript.ScriptFullName).ParentFolder.ParentFolder
Wscript.Echo strPath
7.拷贝文件
sfile = "D:\cc\cc\1.txt"
dfile = "D:\cc\dd\1.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile sfile,dfile, true
//true代表覆盖写,false不覆盖,若目的文件存在则报错
8.拷贝文件夹内所有到另一个文件夹下
sfile = "D:\cc"
dfile = "D:\dd"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.copyfolder sfolder,dfolder,true
//例如cc下存在1.txt,拷贝后目的文件路径为"D:\dd\1.txt
//若dfile = "D:\dd\",拷贝后目的文件路径为"D:\dd\cc\1.txt
//若sfile = "D:\cc\*",不论dfile = "D:\dd\"、还是dfile = "D:\dd",拷贝时将cc下的子文件夹拷贝到dd目录下
9.检测进程存在
Function isProcessRun(ProcessName)
isProcessRun = False
Set WinMgmtsObj = GetObject("WinMgmts:")
Set ProcessesObj = WinMgmtsObj.InstancesOf("Win32_Process")
For Each Obj In ProcessesObj
If InStr(UCase(ProcessName),UCase(Obj.Description)) <> 0 Then
isProcessRun = True
Exit For
End If
Next
Set ProcessesObj = Nothing
Set WinMgmtsObj = Nothing
End Function
10.检测进程组存在
Function isProcessesRun(ProcessesName)
isProcessesRun = False
Set WinMgmtsObj = GetObject("WinMgmts:")
Set ProcessesObj = WMI.InstancesOf("Win32_Process")
ProcessName=Split(ExeName,"|")
For Each Obj In ProcessesObj
For i=0 to UBound(ProcessName)
If InStr(UCase(ProcessName(i)),UCase(Obj.Description)) <> 0 Then
isProcessesRun = True
Exit For
End If
Next
Next
Set ProcessesObj = Nothing
Set WinMgmtsObj = Nothing
End Function
11.关闭进程
Sub CloseProcess(ProcessName,RunMode)
Set ws = createobject("Wscript.Shell")
ws.run "cmd.exe /C Taskkill /f /im " & ExeName,RunMode
Set ws = Nothing
End Sub
12.关闭进程组
Sub CloseProcessEx(ProcessesName,RunMode)
dim ws,ProcessName,CmdCode,i
ProcessName = Split(ProcessesName, "|")
For i=0 to UBound(ProcessName)
CmdCode=CmdCode & " /im " & ProcessName(i)
Next
Set ws = createobject("Wscript.Shell")
ws.run "cmd.exe /C Taskkill /f" & CmdCode,RunMode
Set ws = Nothing
End Sub
13.msgbox
msgbox("Do you love me ?", vbYesNo, "LOVE")//内容,按钮是/否,标题
14.wscript.echo
wscript.echo "hello world."
15.读取ini
Function getOperationType(iniPath , sectionName ,keyName)
Dim objFSO , objFile ,textLine , tmpArray
Dim operationType:operationType= "Not Found"
Dim status:status=0
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.fileExists(iniPath) Then
Exit Function
End If
Set objFile = objFSO.OpenTextFile(iniPath , 1)
Do Until objFile.AtEndOfStream
textLine = objFile.readline
If InStr(textLine , sectionName) <> 0 And InStr(textLine , "[") And InStr(textLine , "]") <> 0 Then
status = 1
End If
If status = 1 Then
If InStr(textLine , keyName) <> 0 Then
tmpArray = Split(textLine , "=" , -1 , 1)
operationType = Lcase(Trim(tmpArray(1)))
Exit Do
End If
End If
Loop
objFile.close
getOperationType = operationType
Set objFile = Nothing
Set objFSO = Nothing
End Function
16.注册表
Dim OperationRegistry
Set OperationRegistry=WScript.CreateObject("WScript.Shell")
OperationRegistry.RegWrite "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\%appName%",%appPath%
11-23
1464
02-08