How does VBS get the file list in the specified directory

Time:2021-10-7

VBS can get a list of files in a directory

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
dim FileName,fs,foldername
foldername = InputBox("Please enter the folder you want to find", "VBS find file")
If foldername = "" Then
wscript.quit
End If
Set fs = CreateObject("scripting.filesystemobject")
digui (foldername)'call recursive function to find
msgbox FileName 'result display
 
'here is the recursive lookup function
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 'append to variable filename if found
Next
For Each j In subfolders
digui (j.path) 'recursively find subdirectories
Next
End Function

‘the directory list is similar to the above and can be modified slightly.

VBS gets the collection of files and folders in the directory

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Dim sFolder, sExt, message
sFolder = "F:\Programming\Applications\VBScript"
 
Dim fs, oFolder, oFiles, oSubFolders
set fs = CreateObject("Scripting.FileSystemObject")
set oFolder = fs.GetFolder(sFolder)   'get folder
set oSubFolders = oFolder.SubFolders  'get subdirectory collection
 
for each folder in oSubFolders
  message = Folder: & folder
  MsgBox message
Next
 
set oFiles = oFolder.Files       'get file collection
for each file in oFiles
  sExt = fs.GetExtensionName(file)  'get file extension
  sExt = LCase(sExt)         'convert to lowercase
  message = File name: & file.Name & , extension: & sExt 'get the file name (including extension, excluding path) and extension
  MsgBox message
Next

For example, the folder.name above can get the name of the folder (excluding the path)
For example: folder = f: \ programming \ applications \ VBScript \ DD
You can get “DD” through folder.name

Batch statistics of subdirectory files

?
1
2
3
4
5
6
7
@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
)

You can make up the details