VBS中常用指令碼代碼_vbs

來源:互聯網
上載者:User
將域使用者或租添加到本機群組
Set objGroup = GetObject("WinNT://./Administrators")
Set objUser = GetObject("WinNT://testnet/Engineers")
objGroup.Add(objUser.ADsPath)

修改本地管理員密碼
Set objcnlar = GetObject("WinNT://./administrator, user")
objcnla.SetPassword " P@ssW0rd "
objcnla.SetInfo

彈出 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 wso = CreateObject("WScript.Shell") '聲明
wso.RegWrite "%Path%"'建立子鍵
wso.RegWrite "%Path%","%Value%"'修改"預設"索引值
wso.RegWrite "%Path%",%Value%,%RegType% '修改特定類型的索引值
'(字串值 REG_SZ 可擴充字串值 REG_EXPAND_SZ DWORD值 REG_DWORD 二進位值 REG_BINARY)

Set WSHShell= Wscript.CreateObject("Wscript.Shell")
WSHShell.RegRead (%Path%) '讀取註冊表子鍵或索引值(一般用於判斷某一事件是否執行)

Set wso = CreateObject("WScript.Shell")
wso.RegDelete "%Path%" '刪除子鍵或索引值
'(根鍵縮寫HKEY_CLASSES_ROOT HKCR HKEY_CURRENT_USER HKCU HKEY_LOCAL_MACHINE HKLM,其餘無)


程式碼

Set wso = CreateObject("Wscript.Shell")
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1"
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1","0"
wso.RegWrite "HKLMSOFTWAREMicrosftWindows NT#1#2",0,REG_BINARY
wso.RegDelete "HKLMSOFTWAREMicrosftWindows NT#1"
Wscript.quit


檔案的複製/刪除/建立/簡單的寫入
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")


'(由上例可以看出,檔案夾的操作很多是和檔案的操作相通的,因此VBS檔案具有很多命令的統一性)

將某一指定檔案夾的所有唯讀檔案轉為可讀檔案
Const ReadOnly = 1 '設唯讀屬性對應值為1

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

將Word檔案另存新檔文字檔
Const wdFormatText = 2 '設定常數值
(當該值為8時另存新檔HTML文檔,為11時另存新檔XML文檔)
Set objWord = CreateObject("Word.Application") '申明調用函數
Set objDoc = objWord.Documents.Open("%Path%") '開啟某DOC檔案
objDoc.SaveAs "%PATH2%", wdFormatText 另存新檔……
objWord.Quit


程式碼

Const wdFormatText = 2
Set objWord = CreateObject("Word.Application")
Set objDoc = objWord.Documents.Open("d:doc1.doc")
objDoc.SaveAs "g:doc1.txt", wdFormatText
objWord.Quit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.