彈出 YES or NO 的對話方塊,不同的選擇執行不同的代碼
intAnswer = Msgbox("Do you want to delete these files?", vbYesNo,
"Delete Files")
If intAnswer = vbYes Then
Msgbox "You answered yes."
Else Msgbox "You answered no."
End If
運行CMD命令列命令
set obshell=wscript.createobject("wscript.shell")
obshell.run ("ipconfig"),,true
如果要啟動並執行命令中包含雙引號,可使用&chr(34)&代替
忽略代碼錯誤繼續執行
On Error Resume Next
放置於代碼的最開頭,當代碼運行出錯後並不停止跳出而是繼續執行下一條。適當應用會很有效果。
檔案的複製/刪除/建立/簡單的寫入
Set fso = Wscript.CreateObject("Scripting.FileSystemObject") '聲明
Set f = fso.CreateTextFile("%PATH%") '建立檔案,其中f可任意,包含縮減名
f.WriteLine("VBS") '寫檔案內容,該命令功能太簡單,目前看來只能用於TXT檔案
f.Close
set c=fso.getfile("%path%") '拷貝某檔案
c.copy("%PATH2%") '拷貝檔案到指定地點
fso.deletefile("%PATH%") '刪除檔案
Wscript.quit
程式碼
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set f=fso.CreateTextFile("C:Sample.txt")
WriteLine("VBS")
f.close
set e=fso.getfile(C:Sample.txt)
e.copy("D:Sample.txt")
fso.deletefile(C:Sample.txt)
Wscript.quit
嚮應用程式輸出簡單的連串指令
dim program1 '聲明變數program1
program1= "%Path%" '應用程式路徑
set wshshell=createobject("wscript.shell") '聲明飲用函數
set oexec=wshshell.exec(program1) '運行程式
wscript.sleep 2000 '(該行命令未知作用.估計是設定延遲,請高手指點)
wshshell.appactivate "%WindowsName%" '啟用運用程式視窗
wshshell.sendkeys "+{%KeyBoardName%}" '第一次輸出鍵盤按鍵指令前要加+
wshshell.sendkeys "555555" '在程式輸入欄中輸入運用該系列命令須首先確定程式可以實施連串的鍵盤操作,這在QQ登入中最適用,如下例。
程式碼
dim program1
program1="D:Program FilesTencentcoralQQ.exe"
set wshshell=CreateObject("wscript.shell")
set oexec=wshshell.exec(program1)
wscript.sleep 2000
wshshell.appactivate "QQ登入"
wshshell.sendkeys "+{TAB}"
wshshell.sendkeys "250481892"
wscript.sleep 2000
wshshell.sendkeys "{TAB}"
wshshell.sendkeys "****************"
wscript.sleep 2000
wshshell.sendkeys "{ENTER}"
Wscript.quit
檔案夾的簡單操作
Set fso = Wscript.CreateObject("Scripting.FileSystemObject") '聲明
Set f = fso.CreateFolder("%PATH%") 建立檔案夾
Set e = getFolder(%PATH%) 類似於"繫結目標"
e.copy("%PATH2%") 複製檔案夾
fso.deletefolder(%PATH%) 刪除檔案夾
程式碼
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateObject("C:sample")
f.copy("D:sample")
fso.deletefolder("C:sample")
Set FSO = CreateObject("Scripting.FileSystemObject") '聲明
Set Folder = FSO.GetFolder("%PATH%") '繫結檔案夾
Set colFiles = Folder.Files '檔案夾所有檔案
For Each objFile in colFiles '下列語句應用於檔案夾所有檔案
If File.Attributes AND ReadOnly Then '這是關鍵之處,這裡應用了If判斷語句,來檢測檔案屬性是否為唯讀
File.Attributes = File.Attributes XOR ReadOnly
'對判斷結果為Ture(預設為True)'執行XOR邏輯運算,將其改為可讀
End If '結束判斷
Next