加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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)
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:\abc"


'创建多个文件'
Set fso = CreateObject("scripting.filesystemobject")
For i =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 To Len(str) -Step 8
    str2 Mid(str,i,8)
    
f1.WriteLine str2
Next



'产生随机数N
Randomize
Fix(Rnd * 60)

'发送键盘TAB N次
For index =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 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(NumF1.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随机输入字符到网页对话框'

'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 /(num%% = 16)
start C: \ New \ %num%.vbs


生成随机字符
━━━━━━━━━━━━━━━━━━━━━━━━━

Function NumRand(n'生成n位随机数字
    
For i =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 =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 =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 =To n
        Randomize
        
temp CInt(25 * Rnd)
        
If temp Mod Then
            
temp temp 97
        ElseIf temp Then
            
temp temp 48
        Else
            
temp temp 65
        End If
        
allRand allRand Chr(temp)
    
Next
End Function
MsgBox 
allRand(5),,"生成n位随机数字字母子组合"

把以上内容存为rand.vbs双击运行,可以看到效果。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有