当前位置:网站首页>Vbs script COM object extension and use (3)
Vbs script COM object extension and use (3)
2022-07-25 06:06:00 【N-order magic cube】
CreateObject, This command can access windows All installed in the system com object , And you can call the commands stored in these parts .
WSH It is used to parse Vbs Host of , It contains several common objects :
1、Scripting.FileSystemObject —> Provide a complete set of file system operation functions
2、Scripting.Dictionary —> The dictionary object used to return the key value pair
3、Wscript.Shell —> Provide a set of functions to read system information
( Such as reading and writing registry 、 Find the path to the specified file 、 Read DOS environment variable , Read the settings in the link )
4、Wscript.NetWork —> Provides functions for network connection and remote printer management .
notes : all Scripting Objects are stored in SCRRUN.DLL In file , be-all Wscript Objects are stored in WSHOM.ocx In file .
routine :
option Explicit ‘ All variables are executed before using options
dim objShell
' Object values must use SET Keyword assignment
set objShell = CreateObject("WScript.Shell")
' Nonblocking call , Sequential execution
objShell.Run "notepad"
' Blocking call ,APP Execute one after closing
objShell.Run "notepad" ,,true
' Nonblocking call , Turn on the calculator
objShell.Run "calc"
' notes : Avoid the problems caused by spaces , Use quotation marks ""
'Run " The program path ", Window style (0 Background operation ;1 The normal operation ;2 Activate minimize ;3 Activate maximize ;.....), Operation waiting (True)
objshell.Run """C:\Program Files\QQ2006\QQ.exe"""Two 、 Error handling
When the script does not work properly , Will cause runtime errors , ... will be displayed on the screen MsgBox.
On Error Resume Next
This line tells vbs Skip the statement with error at run time , Then execute the statement following it .
When something goes wrong , This statement will put the relevant error number 、 The error description and related source code are pushed into the error stack .
vbscript Provides a err object
Two methods clear,raise,
5 Attributes :description,helpcontext,helpfile,number,source
err Objects do not reference instances , You can use it directly , for example :
on error resume next
a=11
b=0
c=a/b
if err.number<>0 then
wscript.echo err.number & err.description & err.source
end if
3、 ... and 、 Registry operation
1、 Read registry Wscript.Shell.Regread
set ws = wscript.createobject("Wscript.Shell")
value = ws.regread("HKLM\Software\7-Zip\Path ")
wscript.echo value
2、 Write registry Wscript.Shell.Regwrit
path="HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run\"set ws=wscript.createobject("wscript.shell")
t=ws.regwrite(path & "jj","hello")
' Create a new value
val=ws.regwrite(path,"newboy")
notes : Run\jj This key value is changed to hello.( The key value must exist in advance )
3、 Delete keywords and values Wscript.Shell.Regdelete
val=ws.Regdelete(path)
Four 、 File system operations ( important )
SET FSOobj = Wscript.createobject("Scripting.FileSystemObject")
1、FSO Common objects included are :
Drive object : Contains information about storage devices , Including hard disk 、 CD drive 、ram disc 、 Network drive
Drives aggregate : Provide a list of physical and logical drives
File object : Check and process documents
Files aggregate : Provide a list of files in the folder
Folder object : Check and handle folders
Folders aggregate : Provide a list of subfolders in the folder
Textstream object : Read write text file
FSO The common methods are :
BulidPath: Add the file path information to the existing file path
CopyFile: Copy file
CopyFolder: Copy folder
CreateFolder: Create folder
CreateTextFile: Create text and return a TextStream object
DeleteFile: Delete file
DeleteFolder: Delete the folder and all its contents
DriveExits: Determine whether the drive exists
FileExits: Determine whether a file exists
FolderExists: Determine whether a folder exists
GetAbsolutePathName: Return the absolute path of a folder or file
GetBaseName: Return the basic path of a file or folder
GetDrive: Return to one dreve object
GetDriveName: Returns the name of a drive
GetExtensionName: Return extension
GetFile: Return to one file object
GetFileName: Return the file name in the folder
GetFolder: Return a folder object
GetParentFolderName: Return to the parent folder of a folder
GetSpecialFolder: Returns an object pointer to a special folder
GetTempName: Returns one that can be createtextfile The name of the randomly generated file or folder used
MoveFile: Moving files
MoveFolder: Move folder
OpenTextFile: Open an existing file and return a TextStream object
2、 Folder operation, etc
' Creating models
DIM objFS
SET objFS = Wscript.createobject(“Scripting.FileSystemobject”)
' Release model
'SET objFS = nothing
if ( objFS.FolderExists("c:\temp") ) ' Check whether the folder is large
objFS.Deletefolder("c:\temp") ' Delete folder
objFS.CopyFolder "c:\data" "d:\data",'True ' Copy folder , Overlay options
objFS.movefolder "c:\data\te*" "d:\data\" '\ Automatically create d:\data Folder
objFS.getspecialfolder(0) ' return windows Catalog
objFSgetspecialfolder(1) ' return system32\
objFSgetspecialfolder(2) ' Return to the temporary directory 3、 File operation, etc. stay windows in , The attributes of files are generally expressed in numbers :
0 representative normal, That is, ordinary files do not set any properties . 1 Represents a read-only file .
2 Represents hidden files . 4 Represents system files . 16 Represents a folder or directory .
32 On behalf of archived documents . 1024 Represents a link or shortcut .
Column attribute ( The column number of the current character position ) and line attribute ( Current line number of the file ): After opening a file , Both row and column pointers are set to 1.
' Check the creation file
set fso = wscript.createobject(“scripting.filesystemobject”)
if fso.fileexists(“c:\kk.txt”) then
msgbox " file already exist "
else
set f = fso.createtextfile("c:\kk.txt" ', true ' ) 'true Parameters : Overwrite existing files
end if
fso.copyfile "c:\kk.txt","d:\1\kk.txt", true 'rue Represents mandatory override
fso.movefile "c:\kk.txt", "d:\" ' Moving files
fso.deletefile "c:\kk.txt" ' Delete file
set ts = fso.opentextfile(“c:\kk.txt”,1,true) ' Open file :1 As read-only 、2 write in 、8 To add
set ffile=fso.opentextfile(“c:\kk.txt”,1,true)
value=ffile.read(20) 'read(x) read x Characters
line=ffile.readline 'readline Read a line
contents=ffile.readall 'readall Read all
do while ffile.atendofstream<>true ' At the end of the line , This property returns true.
ffile.read(10)
loop skip(x) skip x Characters ;skipline Skip a line
write(x) write in x character string ;
writeline(x) write in x The line represented
writeblanklines(n) write in n Empty rows
lose Method to close the file , Be sure to close after reading the file , Can be opened in writing .
边栏推荐
猜你喜欢

Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql

(Niuke multi School II) G-LINK with monotonic subsequence (construction question)

HTB-Granpa

SAP FICO section III BDC and ltmc import S4 financial account

node.express中req.body总是undefind解决

sqlilabs less-29

Data too long for column 'data' at row 1 and the garbled code caused by setting to longblob are solved. node-mysql
This is how the permission system is designed, yyds

【Node】服务端口被占用Error: listen EADDRINUSE: address already in use :::9000-如何关闭node启动的端口
![[QT] solve the problem of Chinese garbled code output from QT console](/img/09/8af91d2a0327bd1d3c7b64f2b8185f.png)
[QT] solve the problem of Chinese garbled code output from QT console
随机推荐
基于ISO13209(OTX)实现EOL下线序列
Please stop using system The currenttimemillis() statistical code is time-consuming, which is really too low!
Function template learning record
leetcode/ 前 n 个数字二进制中 1 的个数
(14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动
PMP Exam is easy to confuse concept discrimination skills! Don't lose points after reading!
求求你别再用 System.currentTimeMillis() 统计代码耗时了,真的太 Low 了!
(Niuke multi school I in 2022) i-chiitoitsu (expected DP)
Date (day 76)
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
Ceres solver version 1.14 and eigen3.2.9
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
Use abp Zero builds a third-party login module (4): wechat applet development
暑期总结2
Baidu, Alibaba, Tencent, who fell first?
New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
动态规划学习笔记
Binary search tree (day 75)
Sword finger offer 54. the k-th node of the binary search tree