推荐

VBS读写文件及创建、删除文件

2015-01-15 09:59
set fso=createobject("scripting.filesystemobject")
set zsc=createobject("scripting.dictionary")

if (fso.fileexists("a.txt")) then
'打开文件,参数1为forreading,2为forwriting,8为appending
set file=fso.opentextfile("a.txt",1,ture)

else
'创建文件,参数1为forreading,2为forwriting,8为appending
set file=fso.createtextfile( "a.txt",2,ture)

'写入文件内容,有三种方法:write(x)写入x个字符,writeline写入换行,writeblanklines(n)写入n个空行
file.writeline "welcome!"
file.writeline "thanks!"

set file=fso.opentextfile("a.txt",1,ture)
end if

'读取文件内容,有三种方法:read(x)读取x个字符,readline读取一行,readall读取全部
'读取文件时跳行:skip(x) 跳过x个字符,skipline 跳过一行
do while file.atendofstream<>true
line=line+1
zsc.add line,file.readline

'对话框显示
msgbox "第" & line & "行: " & zsc(line) &"内容"
loop

'关闭文件
file.close

'运行CMD创建文件
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:bc"
 
 
'创建多个文件'
set fso=createobject("scripting.filesystemobject")
for i=1 to 9999
set file=fso.createtextfile("c:a"&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("..txt",ForReading,true)
str = f1.ReadLine
set f1 = FSO.OpenTextFile("..txt",ForWriting ,true)
for i = 1 to len(str)-1 step 8
  str2 = mid(str,i,8)
  f1.WriteLine str2
Next
 
 
 
'产生随机数N
randomize
N = fix(rnd*60)

'发送键盘TAB N次
for index=1 to N
wshshell.SendKeys "{TAB}"
next
 
'vbs随机换桌面'
'服务器路径,只支持BMP格式
ServerPath="\servershare$mp"
'本地文件名,将从服务器复制到本地后改名为这个文件名(包括完整路径)
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_USERControl PanelDesktopTileWallpaper","0","REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USERControl PanelDesktopWallpaper",BMPname,"REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USERControl PanelDesktopWallpaperStyle","2","REG_SZ"
WshShell.RegWrite "HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionExplorerAdvancedListviewShadow","1","REG_DWORD"
'如果桌面图标未透明,需要刷新组策略,如果已经透明,只需要刷新桌面
WshShell.run "gpupdate /force",0
'WshShell.run "RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters"
 
 
'VBS随机输入字符到网页对话框'
 
'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 = "自动输入"
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 "~" ' 回车
'wscript.Sleep 5000
'Wshell.SendKeys "^W" ' 关闭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: ew%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位随机小写字母
  For i=1 to n
    Randomize
    temp = cint(25*Rnd)
    temp = temp +97
    LCharRand = LCharRand & chr(temp)
  Next
End Function
msgbox LCharRand(5),,"生成n位随机小写字母"


Function UCharRand(n) '生成n位随机大写字母
  For i=1 to n
    Randomize
    temp = cint(25*Rnd)
    temp = temp +65
    UCharRand = UCharRand & chr(temp)
  Next
End Function
msgbox UCharRand(5),,"生成n位随机大写字母"


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双击运行,可以看到效果。

^