Home > Software engineering >  List of files VBA under the specified file path
List of files VBA under the specified file path

Time:11-15

Business requirements: the folder (which contains a folder) batch type specified file in
Solution: by VBA file list list under the specified file path, and through a one-dimensional array stored file name, file extensions, list of child folders can be obtain by means of recursive calls file list, and then as a condition of judgment, depending on the type of the file to open a file type for subsequent processing, such as the textual substitution, etc., under the given folder traversal process code:
Sub GetAllFiles (ByVal RecepPath As String) 'get files list
Dim Mainfolder SubFolder, File_Currentfolder As Object
On the Error Resume Next
The Set Mainfolder=FS. Getfolder (RecepPath)
For Each File_Currentfolder In Mainfolder. Files
FilesList_i=FilesList_i + 1
If Right (RecepPath, 1) & lt;> "" Then
RecepPath=RecepPath & amp; ""
End the If
FilesList (FilesList_i, 1)=RecepPath & amp; File_Currentfolder. Name
FilesNameList (FilesList_i, 1)=FS. Getbasename (File_Currentfolder)
FileExtNameList (FilesList_i, 1)=FS. Getextensionname (File_Currentfolder)
Next
For Each SubFolder In Mainfolder. Subfolders
Call GetAllFiles (SubFolder. Path)
Next
End Sub

CodePudding user response:

This is to ask questions? Or ask the answer?

CodePudding user response:

The couple said look not to understand ah ~ ~

CodePudding user response:

Baidu, you know

CodePudding user response:

Look not very know what you mean, but the following code can extract all files folder name, and then you according to the requirements accordingly processing

Sub extract all the filename () within a specified folder 'file containing all subfolders within
Dim Fso As Object, arrf $(), mf&
The Set of Fso=CreateObject (" Scripting. FileSystemObject ")
Call GetFiles (CreateObject (" Shell. Application "). BrowseForFolder (0, "please select a folder," 0, "mypath"). The Self. The Path, Fso, arrf, mf)
[a2]. Resize (mf)=Application. The Transpose (arrf)
The Set of Fso=Nothing
MsgBox (" reads ")
End Sub

CodePudding user response:

The
reference 4 floor & gt; _ & amp; lt; S response:
look is not very understand your meaning, but the following code can extract all files folder name, and then you according to the demand for the corresponding processing

Sub extract all the filename () within a specified folder 'file containing all subfolders within
Dim Fso As Object, arrf $(), mf&
The Set of Fso=CreateObject (" Scripting. FileSystemObject ")
Call GetFiles (CreateObject (" Shell. Application "). BrowseForFolder (0, "please select a folder," 0, "mypath"). The Self. The Path, Fso, arrf, mf)
[a2]. Resize (mf)=Application. The Transpose (arrf)
The Set of Fso=Nothing
MsgBox (" reads ")
End Sub

To the code can give a complete, getfile () function? So the landlord how know

CodePudding user response:

The
Quote: 4 reference building & gt; _ & amp; lt; S response:

Sorry, forgot to put GetFiles function, is the following:
Private Sub GetFiles (ByVal sPath $, ByRef Fso As Object, ByRef arrf $(), ByRef mf&)
Dim Folder As Object
Dim SubFolder As Object
Dim the File As Object
Set the Folder=Fso. Getfolder (sPath)
For Each File In Folder. The Files
Mf=mf + 1
ReDim Preserve arrf (1 To mf)
Arrf (mf)=File. The Path
Next
For Each SubFolder In the Folder. The subfolders
Call GetFiles (SubFolder. Path, Fso, arrf, mf)
Next
Set the Folder=Nothing
Set the File=Nothing
End Sub
  •  Tags:  
  • VBA
  • Related