国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
[寄存]VBS對(duì)文件文件夾操作的例子
返回總目錄  

原作者:we似曾相識(shí)
原地址:http://bbs.anjian.com/thread-208704-1-1.html

//=====================<VBS對(duì)文件夾操作例子>
(1)創(chuàng)建文件夾

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

(4)獲取指定目錄下所有文件的文件名(不包含文件夾名)

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

(5)分離路徑中的文件名(不帶擴(kuò)展名)

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ì)文件操作例子>
(1)創(chuàng)建文件

CODE:

Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("c:\123.txt", True) 'true則可以覆蓋已有的同名文件
Msgbox "這是一個(gè)測(cè)試"
MyFile.Close  'close是必要的,不要省

(2)刪除文件

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

(4)創(chuàng)建快捷方式的例子

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

(5)獲取文件創(chuàng)建訪問等信息

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盤

(8)將某文件夾下所有指定格式文件拷貝到其他目錄

CODE:

Const OverwriteExisting = True
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\*.txt" , "D:\" ,OverwriteExisting

VBS讀取、修改.INI文件鍵值
(1)VBS讀取.INI文件鍵值

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

(2)VBS修改.INI文件鍵值

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

//===============<得到文件、文件夾字節(jié)數(shù)>
[VBS得到目標(biāo)文件字節(jié)數(shù)]

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

//===============<得到文件、文件夾類型>
[VBS得到目標(biāo)文件類型信息]

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

[VBS得到目標(biāo)文件類型信息]

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

//===============<讀取文本內(nèi)容>
[讀取文本全部?jī)?nèi)容、讀取文本行]

CODE:

set ttfile=fso.opentextfile(f,forappending)
'讀取打開文件的一行并回車(下次讀從下一行開始)
read=ttfile.ReadLine
'讀取所有文件內(nèi)容
read=ttfile.ReadAll

[讀取文本返回行數(shù), 指定行內(nèi)容]

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

[倒序輸出文本行內(nèi)容]

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
.
[獲取指定文件的文件名、文件擴(kuò)展名、完整文件路徑等等]

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

[枚舉文件夾屬性、詳細(xì)信息]
'返回字節(jié)數(shù)、目錄、文件夾名稱、文件夾創(chuàng)建訪問修改時(shí)間、路徑、所在驅(qū)動(dòng)盤等

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


[檢索計(jì)算機(jī)上所有隱藏文件夾路徑]

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 顯示

//============<遍歷用戶桌面文件夾、文件>
'[1] 遍歷當(dāng)前用戶(不全)桌面文件

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
'
'[2] 遍歷所有用戶桌面文件
本帖隱藏的內(nèi)容需要回復(fù)才可以瀏覽'
'[3] 獲取桌面路徑,GetFolder方法遍歷子文件夾、文件

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
//============<遍歷盤符,遞歸遍歷子目錄>

'[1] 遍歷計(jì)算機(jī)磁盤名稱

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)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
vbs的一些用法
VBA文件及文件夾操作
【代碼合集】VBA操作文件夾代碼合集
ASP FSO文件操作
VBS常用腳本 好東西
FSO組件之文件夾操作(ASP)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服