彈出 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