Common script code _vbs in VBS

Source: Internet
Author: User
Add a domain user or lease to a local group
Set Objgroup = GetObject ("Winnt://./administrators")
Set objuser = GetObject ("Winnt://testnet/engineers")
Objgroup.add (Objuser.adspath)

Modify Local Administrator password
Set objcnlar = GetObject ("winnt://./administrator, user")
Objcnla. SetPassword "P@ssw0rd"
Objcnla. SetInfo

A dialog box pops up YES or NO, and different options execute different code
Intanswer = Msgbox ("Do your want to delete these files?", vbYesNo,
"Delete Files")
If intanswer = vbyes Then
Msgbox "You answered yes."
Else Msgbox "You answered no."
End If

Running the cmd command line command
Set Obshell=wscript.createobject ("Wscript.Shell")
Obshell.run ("ipconfig"), true
If you want to run a command that contains double quotes, use &CHR & instead

Ignore code error continue execution
On Error Resume Next
Placed at the very beginning of the code, does not stop when the code is running, but continues the next one. Proper application can be very effective.

Registry modification/Read/delete/create
Set WSO = CreateObject ("Wscript.Shell") ' declaration
Wso. RegWrite "%Path%" creates subkeys
Wso. RegWrite "%Path%", "%value%" modifies "default" key value
Wso. RegWrite "%Path%",%value%,%regtype% ' modifies a specific type of key value
' (String value REG_SZ Expandable String value REG_EXPAND_SZ DWORD value REG_DWORD Binary value REG_BINARY)

Set wshshell= wscript.createobject ("Wscript.Shell")
WshShell.RegRead (%Path%) ' Reads a registry subkey or key value (generally used to determine whether an event is performed)

Set WSO = CreateObject ("Wscript.Shell")
Wso. RegDelete "%Path%" deletes subkeys or key values
' (root key abbreviation HKEY_CLASSES_ROOT HKCR HKEY_CURRENT_USER HKCU HKEY_LOCAL_MACHINE HKLM, none remaining)


Program code

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


File copy/delete/create/simple Write
Set FSO = WScript.CreateObject ("Scripting.FileSystemObject") ' declaration
Set f = fso. CreateTextFile ("%PATH%") ' creates the file, where f can be arbitrary, contains the thumbnail name
F.writeline ("VBS") ' writes the file content, the command function is too simple, at present appears only for TXT file
F.close
Set C=fso.getfile ("%path%") ' Copy a file
C.copy ("%path2%") ' Copy file to designated location
Fso.deletefile ("%PATH%") ' Delete file
Wscript.Quit


Program code

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


To output a simple string of instructions to an application
Dim program1 ' Declare Variable program1
Program1= "%Path%" Application path
Set Wshshell=createobject ("Wscript.Shell") ' declares a drinking function
Set Oexec=wshshell.exec (PROGRAM1) ' Run program
Wscript.Sleep 2000 ' (The line command is not known to function.) estimate is set delay, please master point
Wshshell.appactivate "%windowsname%" Activates the application window
Wshshell.sendkeys "+{%keyboardname%}" to be added + before first output keyboard key instruction
Wshshell.sendkeys "555555" in the Program input field to enter the use of this series of commands must first determine that the program can implement a series of keyboard operations, which is most applicable in QQ login, the following example.


Program code

Dim program1
program1= "D:program FilesTencentcoralQQ.exe"
Set Wshshell=createobject ("Wscript.Shell")
Set Oexec=wshshell.exec (PROGRAM1)
Wscript.Sleep 2000
Wshshell.appactivate "QQ Login"
Wshshell.sendkeys "+{tab}"
Wshshell.sendkeys "250481892"
Wscript.Sleep 2000
Wshshell.sendkeys "{tab}"
Wshshell.sendkeys "****************"
Wscript.Sleep 2000
Wshshell.sendkeys ' {ENTER} '
Wscript.Quit


Simple operation of a folder
Set FSO = WScript.CreateObject ("Scripting.FileSystemObject") ' declaration
Set f = fso. CreateFolder ("%PATH%") Create a folder
Set e = GetFolder (%PATH%) Similar to "binding target"
E.copy ("%path2%") Copy folder
Fso.deletefolder (%PATH%) Delete Folder


Program code

Set FSO = WScript.CreateObject ("Scripting.FileSystemObject")
Set f = fso. CreateObject ("C:sample")
F.copy ("D:sample")
Fso.deletefolder ("C:sample")


' (as can be seen from the example above, many of the folders are connected to the operation of the file, so the VBS file has a lot of command uniformity.)

To convert all read-only files for a specified folder to a readable file
Const ReadOnly = 1 ' Set read-only property corresponds to a value of 1

Set FSO = CreateObject ("Scripting.FileSystemObject") ' declaration
Set Folder = FSO. GetFolder ("%PATH%") ' Bind folder
Set colfiles = folder.files ' folder all files

For every objfile in Colfiles ' The following statement applies to all files in the folder
If File.attributes and ReadOnly Then ' This is the key, the IF judgment statement is applied here to detect whether the file property is read-only
File.attributes = file.attributes XOR ReadOnly
' Ture (default is True) ' performs an XOR logical operation and changes it to readable
End If ' Ending judgment
Next

Save a Word file as a text file
Const Wdformattext = 2 ' Set constant value
(Save as HTML document when this value is 8, save as XML document when 11)
Set objword = CreateObject ("Word.Application") ' Affirms calling function
Set objdoc = ObjWord.Documents.Open ("%Path%") ' opens a doc file
Objdoc.saveas "%path2%", Wdformattext Save As ...
Objword.quit


Program code

Const Wdformattext = 2
Set objword = CreateObject ("Word.Application")
Set objdoc = ObjWord.Documents.Open ("D:doc1.doc")
Objdoc.saveas "G:doc1.txt", Wdformattext
Objword.quit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.