VBS读写文件及创建、删除文件
(2014-12-17 22:07:14)
标签:
it |
分类: ◆VB.NET_(批处理) |
VBS读写文件及创建、删除文件
━━━━━━━━━━━━━━━━━━━━━━━━━
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)
EndIf
'读取文件内容,有三种方法: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:\abc"
'创建多个文件'
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
'产生随机数N
Randomize
N= Fix(Rnd * 60)
'发送键盘TABN次
For index = 1 To N
wshshell.SendKeys "{TAB}"
Next
'vbs随机换桌面'
'服务器路径,只支持BMP格式
ServerPath = "\\server\share$\bmp"
'本地文件名,将从服务器复制到本地后改名为这个文件名(包括完整路径)
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)
ForEach 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"
'如果桌面图标未透明,需要刷新组策略,如果已经透明,只需要刷新桌面
WshShell.run"gpupdate /force ",0
'WshShell.run "RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters "
'VBS随机输入字符到网页对话框'
'sethuobilie = 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.Sleep2000
ie.navigate"www.baidu.com"
SynchronizeIE()
Set wshshell = CreateObject("wscript.shell")
wscript.sleep500
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.Sleep500
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.Sleep5000
'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的脚本
@echooff
Set num = %RANDOM%
Set / A (num%% =
16)
startC:
\ 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
EndFunction
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
EndFunction
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
EndFunction
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
EndFunction
MsgBox allRand(5),,"生成n位随机数字字母子组合"
把以上内容存为rand.vbs双击运行,可以看到效果。
━━━━━━━━━━━━━━━━━━━━━━━━━
Set
Set
If
Else
End
'读取文件时跳行:skip(x)
Loop
'关闭文件
file.Close
'运行CMD创建文件
var
Set
ws.Run
'删除文件及文件夹:
fso.deleteFile
fso.deleteFolder
'创建多个文件'
Set
For
Next
'把123132132156464654574654
Const
Set
Set
str
Set
For
Next
'产生随机数N
Randomize
N
'发送键盘TAB
Next
'vbs随机换桌面'
'服务器路径,只支持BMP格式
ServerPath = "\\server\share$\bmp"
'本地文件名,将从服务器复制到本地后改名为这个文件名(包括完整路径)
BMPname = "e:\crt.bmp"
Set
Set
Set
Set
Num
ReDim
For
Next
Randomize
WshShell.RegWrite
WshShell.RegWrite
WshShell.RegWrite
WshShell.RegWrite
'如果桌面图标未透明,需要刷新组策略,如果已经透明,只需要刷新桌面
WshShell.run
'WshShell.run
'VBS随机输入字符到网页对话框'
'set
'huobilie.run
ie.Navigate
ie.ToolBar
ie.StatusBar
ie.Width = 400
ie.Height
ie.Left
ie.Top
SynchronizeIE()
ie.Visible
ie.Document.Body.InnerHTML
WScript.Sleep
ie.navigate
SynchronizeIE()
Set
wscript.sleep
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
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
'Wshell.SendKeys
'//等待IE操作结束。
@echo
Set
Set
start
生成随机字符
━━━━━━━━━━━━━━━━━━━━━━━━━
Function
End
MsgBox
Function
End
MsgBox
Function
End
MsgBox
Function
End
MsgBox
把以上内容存为rand.vbs双击运行,可以看到效果。