1. 程式人生 > 程式設計 >VBS怎麼獲取指定目錄下的檔案列表

VBS怎麼獲取指定目錄下的檔案列表

VBS腫麼獲取某目錄下的檔案列表

dim FileName,fs,foldername
foldername = InputBox("請輸入想要在哪個資料夾查詢","VBS查詢檔案")
If foldername = "" Then
wscript.quit
End If
Set fs = CreateObject("scripting.filesystemobject")
digui (foldername)'呼叫遞迴函式進行查詢
msgbox FileName '結果顯示

'下面是遞迴查詢函式
Function digui(path)
Set folder = fs.getfolder(path)
Set subfolders = folder.subfolders
Set Files = folder.Files
For Each i In Files
FileName=FileName & i.path & vbNewLine '找到則追加到變數FileName中
Next
For Each j In subfolders
digui (j.path) '遞迴查詢子目錄
Next
End Function

'目錄列表與上述相似,稍加修改即可。

vbs獲取目錄下的檔案和資料夾集合

Dim sFolder,sExt,message
sFolder = "F:\Programming\Applications\VBScript"
 
Dim fs,oFolder,oFiles,oSubFolders
set fs = CreateObject("Scripting.FileSystemObject")
set oFolder = fs.GetFolder(sFolder)   '獲取資料夾
set oSubFolders = oFolder.SubFolders  '獲取子目錄集合
 
for each folder in oSubFolders
  message = "資料夾:" & folder
  MsgBox message
Next
 
set oFiles = oFolder.Files       '獲取檔案集合
for each file in oFiles
  sExt = fs.GetExtensionName(file)  '獲取副檔名
  sExt = LCase(sExt)         '轉換成小寫
  message = "檔名:" & file.Name & ",副檔名:" & sExt '獲得檔名(含副檔名,不含路徑)和副檔名
  MsgBox message
Next

充,上面的folder.Name可以得到資料夾的名稱(不含路徑)
如:folder = F:\Programming\Applications\VBScript\dd
通過folder.Name可以得到"dd"

批量統計子目錄檔案數量

@echo off&setlocal enabledelayedexpansion
cd.>dirfiles.txt
for /d %%a in (*.*) do (
set n=0
for /f %%B in ('dir /a-d /b /s "%%a"') do set /a n+=1
echo %%a  !n! >>dirfiles.txt
)

具體的大家可以湊湊