CODE:
Dim fso, f
'如果指定的文件夾已經(jīng)存在,則會(huì)出現(xiàn)錯(cuò)誤。
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateFolder("c:\New Folder")
(2)刪除文件夾CODE:
Dim fso,filespec
filespec="D:\電影" '要?jiǎng)h除的文件夾路徑
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(filespec)
'若刪除只讀文件夾則將上一行改為fso.DeleteFolder(filespec,true)
(3)判斷文件夾是否存在CODE:
Dim fso,msg,tt
Set fso = CreateObject("Scripting.FileSystemObject")
fldr="C:\Documents and Settings" '文件夾路徑和名字
tt = fso.FolderExists(fldr) '存在返回true;不存在返回false
If tt=true Then
msg = fldr & " 存在。"
Else
msg = fldr & " 不存在。"
End If
Msgbox msg
CODE:
Dim a
a="D:\文件夾1" '目標(biāo)文件夾完整路徑
Msgbox ShowFolderList(a)
Function ShowFolderList(folderspec)
Dim fso, f, f1, fc, s '定義變量
Set fso = CreateObject("Scripting.FileSystemObject") '創(chuàng)建對(duì)象
Set f = fso.GetFolder(folderspec) '得到文件夾下folder對(duì)象
Set fc = f.Files
For Each f1 in fc '查找所有符合條件的文件名
s = s & f1.name
s = s & vbcrlf '得到結(jié)果并換行
Next
ShowFolderList = s
End Function
CODE:
Msgbox GetTheBase("C:\tt.txt") 'GetTheBase返回值tt
Function GetTheBase(filespec)
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheBase = fso.GetBaseName(filespec)
End Function
//=====================<VBS對(duì)文件操作例子>CODE:
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\123.txt", True) 'true則可以覆蓋已有的同名文件
Msgbox "這是一個(gè)測(cè)試"
MyFile.Close 'close是必要的,不要省
CODE:
Dim fso,filespec
filespec="C:\123.txt" '要?jiǎng)h除的文件
'設(shè)置成"C:\*.doc"則刪除C目錄下所有.doc擴(kuò)展名的文檔(但不會(huì)刪除其子文件夾下.doc文檔)
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile(filespec)
'若刪除只讀文件則將上一行改為fso.DeleteFile(filespec,true)
(3)判斷文件是否存在CODE:
Dim fso,msg,tt
Set fso = CreateObject("Scripting.FileSystemObject")
fle="C:\123.txt" '文件的完整路徑
tt = fso.FileExists(fle) '存在返回true;不存在返回false
If tt=true Then
msg = fle & " 存在。"
Else
msg = fle & " 不存在。"
End If
Msgbox msg
CODE:
'用CreateObject的方法開啟WshShell
Set WshShell=CreateObject("WScript.Shell")
'制定文件夾為桌面
strDesKtop=WshShell.SpecialFolders("DesKtop")
'在制定文件夾創(chuàng)建"畫筆.lnk"快捷方式
Set oShellLink=WshShell.CreateShortcut(strDesKtop&"\畫圖.lnk")
'制定快捷方式指向的目標(biāo)程序
oShellLink.TargetPath="mspaint.exe"
'制定風(fēng)格
oShellLink.WindowStyle=1
'制定熱鍵
oShellLink.Hotkey="CTRL+SHIFT+P"
'制定圖標(biāo)
oShellLink.IconLocation="mspaint.exe,0"
'注釋快捷方式
oShellLink.Description="有標(biāo)準(zhǔn)VBS建立的畫筆快捷方式"
'制定工作目錄
oShellLink.WorkingDirectory=strDesKtop
'保存快捷方式
oShellLink.Save
CODE:
Msgbox FileInfor("C:\b.txt")
Function FileInfor(FilePath)
Dim fso, f, s ,a
Set fso = CreateObject("Scripting.FileSystemObject")
a=FilePath '文件完整路徑
Set f = fso.GetFile(a)
s = f.Path & vbcrlf
s = s & "創(chuàng)建時(shí)間: " & f.DateCreated & vbcrlf
s = s & "訪問時(shí)間: " & f.DateLastModified & vbcrlf
s = s & "修改時(shí)間: " & f.DateLastAccessed
ShowFileAccessInfo = s
FileInfor = s
End Function
(6)VBS獲取特定文件路徑CODE:
Set wshell = CreateObject("WScript.Shell")
PathDesktop = wshell.specialfolders("Desktop")
MsgBox PathDesktop&"=桌面 的絕對(duì)路徑"
PathFavorites = wshell.specialfolders("Favorites")
MsgBox PathFavorites&"=收藏夾 的絕對(duì)路徑"
PathFonts = wshell.specialfolders("Fonts")
MsgBox PathFonts&"=字體 的絕對(duì)路徑"
PathMyDocuments = wshell.specialfolders("MyDocuments")
MsgBox PathMyDocuments&"=我的文檔 的絕對(duì)路徑"
PathNetHood = wshell.specialfolders("NetHood")
MsgBox PathNetHood&"=網(wǎng)上鄰居 的絕對(duì)路徑"
PathPrintHood = wshell.specialfolders("PrintHood")
MsgBox PathPrintHood&"=打印機(jī) 的絕對(duì)路徑"
PathPrograms = wshell.specialfolders("Programs")
MsgBox PathPrograms&"=程序 的絕對(duì)路徑"
PathRecent = wshell.specialfolders("Recent")
MsgBox PathRecent&"=最近文檔 的絕對(duì)路徑"
PathSendTo = wshell.specialfolders("SendTo")
MsgBox PathSendTo&"=發(fā)送到 的絕對(duì)路徑"
PathStartMenu = wshell.specialfolders("StartMenu")
MsgBox PathStartMenu&"=開始菜單 的絕對(duì)路徑"
PathStartUp = wshell.specialfolders("StartUp")
MsgBox PathStartUp&"=啟動(dòng) 的絕對(duì)路徑"
PathTemplates = wshell.specialfolders("Templates")
MsgBox PathTemplates&"=模板 的絕對(duì)路徑"
(7)復(fù)制文件到其他目錄CODE:
Dim sPath, tPath
sPath="C:\tt.txt"
tPath="D:\tt.txt"
set fso=CreateObject("Scripting.FileSystemObject")
fso.CopyFile sPath, tPath '將C:\tt.txt復(fù)制到D盤
CODE:
Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\*.txt" , "D:\" ,OverwriteExisting
CODE:
Msgbox ReadINI("C:\123.ini","c","timeout") '輸入的小節(jié)或鍵名不存在則返回為空
Function ReadINI(FilePath,Bar,PrimaryKey)
Dim fso,sReadLine,i,j,ss
Set fso=CreateObject("Scripting.FileSystemObject")
Set INIfile=fso.opentextfile(FilePath,1)
do until INIfile.atendofstream
sReadLine=INIfile.readline
If sReadLine="" then
INIfile.skipline
ElseIf Trim(sReadLine)="["& Bar &"]" then '找到小節(jié)名
'查找該小節(jié)名下的鍵名
Do until INIfile.atendofstream
sReadLine=INIfile.readline '讀取小節(jié)名后的行
j=instr(sReadLine,"=")
If j>0 then '小節(jié)名后的文本行存在
If instr(Left(sReadLine,j),PrimaryKey)>0 then '從"="左邊字符串找到鍵名
ss = Trim(Right(sReadLine,len(sReadLine)-instr(sReadLine,"=")))
Exit do
Else
INIfile.skipline '沒找到鍵名跳過此行
End If
Else
INIfile.skipline
End If
Loop
Else
End If
loop
INIfile.close
Set fso=nothing
ReadINI = ss
End Function
CODE:
Call WriteINI("C:\123.ini","c","timeout", "abcdef")
'上面一行是調(diào)用方法
Function WriteINI(FilePath, Bar, PrimaryKey, Value)
On Error Resume Next
Const ForReading=1
Const ForWriting=2
Const ForAppending=8
Dim fso, sRead, i, j, sReadLine, BE, stxt
Set fso=CreateObject("Scripting.FileSystemObject")
Set ForRead=fso.opentextfile(FilePath,1)
Dim a() '文本內(nèi)容逐行放入數(shù)組
i=0
j=0
BE=0
sRead=ForRead.ReadAll
ForRead.close
If Instr(sRead,"[")=0 then '說明INI文件沒有主鍵,那么寫入主鍵和鍵值即可
Set ForWrite=fso.opentextfile(FilePath,2)
ForWrite.WriteLine "["& Bar &"]"
ForWrite.WriteLine PrimaryKey &"="& Value
Else
Dim sArray
sArray=split(sRead,"[")
For i=1 to UBound(sArray)
If Left(sArray(i),Len(Bar))=Bar then '小節(jié)名存在
If Instr(sArray(i),PrimaryKey)>0 then '找到鍵名
Set ForRead=fso.opentextfile(FilePath,1)
Do Until ForRead.AtEndOfStream
sReadLine = ForRead.ReadLine
If Instr(sReadLine,"["& Bar &"]")>0 then
While Instr(sReadLine,PrimaryKey)+0<0 '讀到鍵名為止
Msgbox sReadLine
sReadLine = ForRead.ReadLine
Wend
stxt = Right(sReadLine,Len(sReadLine)-Left(sReadLine,instr(sReadLine,"=")))
Exit do
End If
Loop
sArray(i)=Replace(sArray(i), stxt, Value) '將鍵值替換為Value
Else
sArray(i)=Replace(sArray(i),Bar&"]",Bar&"]"& vbcrlf & PrimaryKey &"="& Value)
End If
BE=1
Else
End If
Next
If BE=1 then 'BE=1說明小節(jié)名存在,用數(shù)組配置好了鍵名與鍵值
ForRead.close
Set ForWrite=fso.opentextfile(FilePath,2) '覆蓋原有內(nèi)容重新寫入
ForWrite.Write ""
ForWrite.close
Set ForWrite=fso.opentextfile(FilePath,8) '清空并追加寫入內(nèi)容
i=0
For i=1 to UBound(sArray)
ForWrite.Write "["& sArray(i)
Next
ForWrite.close
Set fso=nothing
Exit Function
End If
ForRead.close '寫入小節(jié)名和鍵值
Set ForWrite=fso.opentextfile(FilePath,8)
ForWrite.WriteLine "["& Bar &"]"
ForWrite.WriteLine PrimaryKey &"="& Value
End If
ForWrite.close
Set fso=nothing
End Function
CODE:
Function ShowFileSize(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = UCase(f.Name) & " 大小為 " & f.size & " 字節(jié)。"
ShowFileSize = s
End Function
[VBS得到目標(biāo)文件夾下所有文件的字節(jié)數(shù)]CODE:
Function ShowFolderSize(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(filespec)
s = UCase(f.Name) & " 大小為 " & f.size & " 字節(jié)。"
ShowFolderSize = s
End Function
CODE:
Function ShowFileType(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filespec)
s = UCase(f.Name) & " 的類型為 " & f.Type
ShowFileType = s
End Function
CODE:
Function ShowFolderType(filespec)
Dim fso, f, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(filespec)
s = UCase(f.Name) & " 的類型為 " & f.Type
ShowFolderType = s
End Function
CODE:
set ttfile=fso.opentextfile(f,forappending)
'讀取打開文件的一行并回車(下次讀從下一行開始)
read=ttfile.ReadLine
'讀取所有文件內(nèi)容
read=ttfile.ReadAll
CODE:
n=inputbox("請(qǐng)輸入要讀取的第n行","提示")
set fso=createobject("scripting.filesystemobject")
set j=fso.opentextfile("c:\??.txt")
i=0
do until j.atendofstream
h=j.readline
i=i+1
loop
msgbox "共有"&i&"行"
j.close
i=0
set j=fso.opentextfile("c:\??.txt")
do until j.atendofstream
h=j.readline
i=i+1
if i=n then
msgbox "第"&n&"行為"&h
end if
loop
j.close
CODE:
Dim arrFileLines()
i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\tt.txt", 1)
Do
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop Until objFile.AtEndOfStream
objFile.Close
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
Wscript.Echo arrFileLines(l)
Next
. CODE:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("按鍵精靈.rar")
Wscript.Echo "Absolute path: " & objFSO.GetAbsolutePathName(objFile)
Wscript.Echo "Parent folder: " & objFSO.GetParentFolderName(objFile)
Wscript.Echo "File name: " & objFSO.GetFileName(objFile)
Wscript.Echo "Base name: " & objFSO.GetBaseName(objFile)
Wscript.Echo "Extension name: " & objFSO.GetExtensionName(objFile)
CODE:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery ("Select * from Win32_Directory where name = 'c:\\Scripts'")
For Each objFolder in colFolders
errResults = objFolder.Uncompress
Wscript.Echo errResults
Next
CODE:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\新建文件夾")
Wscript.Echo "Date created: " & objFolder.DateCreated '創(chuàng)建時(shí)間
Wscript.Echo "Date last accessed: " & objFolder.DateLastAccessed '最后訪問時(shí)間
Wscript.Echo "Date last modified: " & objFolder.DateLastModified '最后修改時(shí)間
Wscript.Echo "Drive: " & objFolder.Drive '所在磁盤
Wscript.Echo "Is root folder: " & objFolder.IsRootFolder '判斷文件夾是否是根文件夾,返回true/false
Wscript.Echo "Name: " & objFolder.Name '所在文件夾
Wscript.Echo "Parent folder: " & objFolder.ParentFolder '父文件夾對(duì)象
Wscript.Echo "Path: " & objFolder.Path '路徑
Wscript.Echo "Short name: " & objFolder.ShortName '短文件名
Wscript.Echo "Short path: " & objFolder.ShortPath '短路徑
Wscript.Echo "Size: " & objFolder.Size '文件大小M
Wscript.Echo "Type: " & objFolder.Type '字節(jié)B
CODE:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from Win32_Directory Where Hidden = True")
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
[檢索Dll插件版本信息]CODE:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Wscript.Echo objFSO.GetFileVersion("c:\windows\system32\scrrun.dll")
CODE:
CreateObject("WScript.Shell").Run "cmd /c attrib -r 指定目錄 /s /d",0
CODE:
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile("C:\a.txt")
f.Attributes = f.Attributes - 2 ' - 2 隱藏, + 2 顯示
CODE:
Const DESKTOP = &H10&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(DESKTOP)
Set objFolderItem = objFolder.Self
Set colItems = objFolder.Items
For Each objItem in colItems
Txt = Txt & objItem.Name & vbcrlf
Next
Msgbox Txt, 64+4096, "結(jié)果"
Set objFolderItem = nothing
Set objFolder = nothing
Set objShell = nothing
'CODE:
Set wshell = CreateObject("WScript.Shell")
PathDesktop = wshell.specialfolders("Desktop")'得到桌面的絕對(duì)路徑
Dim fso, fc
Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.GetFolder(PathDesktop).SubFolders '遍歷子文件夾
SK = vbcrlf & "[文件夾]" & vbcrlf
For Each fls In fs
SK = SK & fls.Name & vbcrlf
Next
Set fc = fso.GetFolder(PathDesktop).Files '遍歷文件夾下的文件
CK = vbcrlf & "[文件]" & vbcrlf
For Each flc In fc
CK = CK & flc.Name & vbcrlf
Next
txt = SK & CK
Msgbox txt, 64+4096, "結(jié)果"
Set wshell = nothing
//============<遍歷盤符,遞歸遍歷子目錄>CODE:
Set fso=CreateObject("scripting.filesystemobject")
Set objdrives = fso.Drives
For Each objdrive In objdrives '遍歷磁盤
TracePrint objdrive
Next
'[2]遞歸遍歷指定目錄下所有子目錄(遍歷目標(biāo)文件夾下所有文件夾、子文件夾路徑)CODE:
Msgbox 遞歸遍歷所有子目錄("D:\")
Function 遞歸遍歷所有子目錄(目錄)
VBSBegin
Function GetSubFolders(sPath)
Set fso = CreateObject("Scripting.FileSystemObject")
Set sFolder = fso.GetFolder(sPath)
Set subFolderSet = sFolder.SubFolders
For Each subFolder in subFolderSet
'MsgBox "subFolder.Path=" & subFolder.Path
GetSubFolders = subFolder.Path & vbcrlf & GetSubFolders(subFolder.Path) & GetSubFolders
Next
End Function
VBSEnd
遞歸遍歷所有子目錄=GetSubFolders(目錄)
End Function
'[3]遞歸遍歷指定目錄下所有子目錄(遍歷目標(biāo)文件夾下所有文件夾、子文件夾路徑)CODE:
Dim AddStr
VBSBegin
Function RecSv(CurrentPath) '遞歸
Set ABC=CurrentPath.subfolders
For Each GGMM In ABC
Set TmpCk=fso.GetFolder(GGMM.path)
AddStr = AddStr & vbcrlf & TmpCk.path
Call RecSv(TmpCk)
Next
RecSv = AddStr
End function
VBSEnd
Set fso=CreateObject("scripting.filesystemobject")
Set SxE=fso.GetFolder("C:\Program Files")
msgbox RecSv(SxE)
聯(lián)系客服