Home > Software engineering >  FSO reading folder files
FSO reading folder files

Time:09-16

A system has file export function, click the export button, you can export the PDF and Excel files in the specified folder,
When there is a process of export, first appear in the folder a 0 to the size of the file, and then the volume of the file will grow big,
I think such documents as the export after completely, send into the outlook as attachments, in practice, there have been some problems,
Teacher, please help me to see that the problem how to solve, thank you

1. Process ListFiles
ListFiles just after click the export button, running, Str $=Obj. Sometimes the Path it complains, the error is "File Not Found",
All attributes of the open window, found that local Obj are all shown as "application - defined or object - defined error",
If after clicking button, wait for a period of time to run ListFiles, won't appear this problem,

2. Process ReadFiles and AccessRight
Using this process, both to one by one to determine whether a folder in the file is in a state of occupied by other program, if the file is not being used all means that all the files are all exported,
But in the actual operation of the process of the two time, I will meet a problem, sometimes, the system itself is an error, the error is "file has been opened by other applications,"
Estimate is just generated files is it occupies the Open FilePath For Binary Lock Read Write As # 1



Sub ListFiles ()
Dim FSO As Object
Dim FSO_Folder As Object
Dim myPath $
Dim Obj
Dim Str $
Dim k1 As Long

MyPath $="C: \ Users \ Desktop \ \ Jim UIAutomation_VBA - master"
The Set of FSO=CreateObject (" Scripting. FileSystemObject ")
The Set FSO_Folder=FSO. GetFolder (myPath)
For Each Obj In FSO_Folder. Files
Str=$Obj. Path
Next Obj
End Sub

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
Sub ReadFiles ()
Dim FSO As Object
Dim FSO_Folder As Object
Dim myPath $
Dim Obj
Dim Str $
Dim k1 As Long

MyPath $="C: \ Users \ Desktop \ \ Jim UIAutomation_VBA - master"
The Set of FSO=CreateObject (" Scripting. FileSystemObject ")
The Set FSO_Folder=FSO. GetFolder (myPath)

Do
K1=0
For Each Obj In FSO_Folder. Files
K1=k1 + AccessRight (Obj. Path)
Next Obj
DoEvents
Loop Until k1=FSO_Folder. Files. Count
End Sub


~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
The Function AccessRight (ByVal FilePath As String) As Long
On Error GoTo The_end

AccessRight=0
The Open FilePath For Binary Lock Read Write As # 1
Close # 1
AccessRight=1

The_end:
End the Function
  • Related