set fso=createobject("scripting.filesystemobject")
set zsc=createobject("scripting.dictionary")
if (fso.fileexists("a.txt")) then
'打開(kāi)文件,參數1為forreading,2為forwriting,8為appending
set file=fso.opentextfile("a.txt",1,ture)
else
'創(chuàng )建文件,參數1為forreading,2為forwriting,8為appending
set file=fso.createtextfile( "a.txt",2,ture)
'寫(xiě)入文件內容,有三種方法:write(x)寫(xiě)入x個(gè)字符,writeline寫(xiě)入換行,writeblanklines(n)寫(xiě)入n個(gè)空行
file.writeline "welcome!"
file.writeline "thanks!"
set file=fso.opentextfile("a.txt",1,ture)
end if
'讀取文件內容,有三種方法:read(x)讀取x個(gè)字符,readline讀取一行,readall讀取全部
'讀取文件時(shí)跳行:skip(x) 跳過(guò)x個(gè)字符,skipline 跳過(guò)一行
do while file.atendofstream<>true
line=line+1
zsc.add line,file.readline
'對話(huà)框顯示
msgbox "第" & line & "行: " & zsc(line) &"內容"
loop
'關(guān)閉文件
file.close
'運行CMD創(chuàng )建文件
var ="cmd to file"
set ws=CreateObject("WScript.Shell")
ws.Run "cmd /c @echo "&var&">>zsc.txt",1
'刪除文件及文件夾:
fso.deleteFile "a.txt"
fso.deleteFolder "F:\abc"
'創(chuàng )建多個(gè)文件'
set fso=createobject("scripting.filesystemobject")
for i=1 to 9999
set file=fso.createtextfile("c:\aa"&i&".txt",true)
file.close
next
'把1231321321564646545746546497978654654數字分為每8位一行'
Dim str,str2
Const ForReading = 1, ForWriting = 2
set FSO = CreateObject("Scripting.FileSystemObject")
set f1 = FSO.OpenTextFile(".\a.txt",ForReading,true)
str = f1.ReadLine
set f1 = FSO.OpenTextFile(".\a.txt",ForWriting ,true)
for i = 1 to len(str)-1 step 8
str2 = mid(str,i,8)
f1.WriteLine str2
Next
'產(chǎn)生隨機數N
randomize
N = fix(rnd*60)
'發(fā)送鍵盤(pán)TAB N次
for index=1 to N
wshshell.SendKeys "{TAB}"
next
'vbs隨機換桌面'
'服務(wù)器路徑,只支持BMP格式
ServerPath="\\server\share$\bmp"
'本地文件名,將從服務(wù)器復制到本地后改名為這個(gè)文件名(包括完整路徑)
BMPname="e:\crt.bmp"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set F = FSO.GetFolder(ServerPath)
Set FC = F.Files
Num = 0
ReDim bmp(FC.Count)
For Each F1 in FC
If UCase(FSO.GetExtensionName(F1.NAME)) = UCase("BMP") then
Num = Num + 1
bmp(Num) = F1.Path
End If
Next
Randomize
FSO.CopyFile bmp(Int(Num * Rnd + 1)),BMPname,True
WshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\TileWallpaper","0","REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\Wallpaper",BMPname,"REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USER\Control Panel\Desktop\WallpaperStyle","2","REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced\ListviewShadow","1","REG_DWORD"
'如果桌面圖標未透明,需要刷新組策略,如果已經(jīng)透明,只需要刷新桌面
WshShell.run "gpupdate /force",0
'WshShell.run "RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters"
'VBS隨機輸入字符到網(wǎng)頁(yè)對話(huà)框'
'set huobilie = Wscript.CreateObject("Wscript.Shell")
'huobilie.run "iexplore http://www.baidu.com"
Set ie = Wscript.CreateObject("InternetExplorer.Application")
ie.Navigate "about:blank"
ie.ToolBar = 0
ie.StatusBar = 0
ie.Width=400
ie.Height = 100
ie.Left = 300
ie.Top = 300
SynchronizeIE()
ie.Visible = 1
ie.Document.Body.InnerHTML = "自動(dòng)輸入"
WScript.Sleep 2000
ie.navigate "www.baidu.com"
SynchronizeIE()
Set wshshell=CreateObject("wscript.shell")
wscript.sleep 500
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wshshell.Sendkeys"{TAB}"
wscript.Sleep 500
wshshell.Sendkeys"a"
wshshell.Sendkeys"d"
wshshell.Sendkeys"m"
wshshell.Sendkeys"i"
wshshell.Sendkeys"n"
wshshell.Sendkeys"{ENTER}"
SynchronizeIE()
ie.Visible=1
'WShell.SendKeys "~" ' 回車(chē)
'wscript.Sleep 5000
'Wshell.SendKeys "^W" ' 關(guān)閉IE窗口
'//等待IE操作結束。
Function SynchronizeIE()
While ie.Busy
WScript.Sleep(100)
Wend
'Do
' Wscript.Sleep 200
'Loop Until ie.ReadyState=4
End Function
下面是bat隨機運行VBS的腳本
@echo off
SET num=%RANDOM%
SET /A (num%%=16)
start C:\new\%num%.vbs
生成隨機字符
Function NumRand(n) '生成n位隨機數字
For i=1 to n
Randomize
temp = cint(9*Rnd)
temp = temp + 48
NumRand = NumRand & chr(temp)
Next
End Function
msgbox NumRand(5),,"生成n位隨機數字"
Function LCharRand(n) '生成n位隨機小寫(xiě)字母
For i=1 to n
Randomize
temp = cint(25*Rnd)
temp = temp +97
LCharRand = LCharRand & chr(temp)
Next
End Function
msgbox LCharRand(5),,"生成n位隨機小寫(xiě)字母"
Function UCharRand(n) '生成n位隨機大寫(xiě)字母
For i=1 to n
Randomize
temp = cint(25*Rnd)
temp = temp +65
UCharRand = UCharRand & chr(temp)
Next
End Function
msgbox UCharRand(5),,"生成n位隨機大寫(xiě)字母"
Function allRand(n) '生成n位隨機數字字母子組合
For i=1 to n
Randomize
temp = cint(25*Rnd)
If temp mod 2 = 0 then
temp = temp + 97
ElseIf temp < 9 then
temp = temp + 48
Else
temp = temp + 65
End If
allRand = allRand & chr(temp)
Next
End Function
msgbox allRand(5),,"生成n位隨機數字字母子組合"
把以上內容存為rand.vbs雙擊運行,可以看到效果。