Microsoft Access 中 WScript 查询的输出结果
Output results of WScript query In Microsoft Access
我有一个 Access 数据库,它使用 WScript 查询来获取用户提供的文件路径的目录信息。我希望能够将结果输出到 SQL 或 Access table 中。
例如,如果我将 WScript 设置为使用 DIR 命令获取 C: 上文件的内容,我希望将其输出放置在 SQL 服务器或访问 table。
我知道 xp_cmdshell 是此类命令的更好选择,但是我们的环境已禁用此功能,没有机会使用它。您可以提供的任何帮助将不胜感激。
谢谢。
您可以使用 FileSystemObject 获取您需要的有关 folders/files 的所有信息。然后,您可以查看文件夹中的所有文件和文件夹中的子文件夹,以填充内存中的 ADO 记录集。将此记录集设置为窗体的记录集。
Public Function GetFolderContents(path As String) As ADODB.Recordset
Dim fso As New FileSystemObject
Dim folderRecordSet As ADODB.Recordset
If Not fso.FolderExists(path) Then
Set GetFolderContents = Nothing
Exit Function
End If
Dim fol As Folder, fil As File, subFol As Folder
Set fol = fso.GetFolder(path)
Set folderRecordSet = GetNewFolderRecordset
For Each fil In fol.Files
If fil.Type <> "System file" Then
folderRecordSet.AddNew
folderRecordSet("Name") = fil.Name
folderRecordSet("Size") = fil.Size
folderRecordSet("Date modified") = fil.DateLastModified
folderRecordSet("Type") = fil.Type
folderRecordSet.Update
End If
Next fil
For Each subFol In fol.SubFolders
folderRecordSet.AddNew
folderRecordSet("Name") = subFol.Name
folderRecordSet("Size") = null
folderRecordSet("Date modified") = subFol.DateLastModified
folderRecordSet("Type") = subFol.Type
folderRecordSet.Update
Next subFol
Set GetFolderContents = folderRecordSet
End Function
Function GetNewFolderRecordset() As ADODB.Recordset
Set GetNewFolderRecordset = New ADODB.Recordset
With GetNewFolderRecordset
.Fields.Append "Name", adVarWChar, 255
.Fields.Append "Size", adInteger, , adFldIsNullable
.Fields.Append "Date modified", adDate
.Fields.Append "Type", adVarWChar, 255
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockPessimistic
.Open
End With
End Function
然后在您的公开赛中,您可以将它们整合在一起。此代码段只是要列出您的临时目录内容。
Private Sub Form_Open(Cancel As Integer)
Set Me.Recordset = Module1.GetFolderContents(Environ("temp"))
End Sub
表单的控件必须绑定到记录集的列,如下所示
当您 运行 它在数据表视图中时,它看起来像这样
您可以捕获适当的 OnClick 事件来打开文件,或者如果 Type = "File Folder"
那么您可以重置记录集以更深一层。您也可以添加人为的 ..
以提高一级。或者制作自己的界面。如果您需要更多,File
和 Folder
对象中还有许多其他 file/folder 属性。
我有一个 Access 数据库,它使用 WScript 查询来获取用户提供的文件路径的目录信息。我希望能够将结果输出到 SQL 或 Access table 中。
例如,如果我将 WScript 设置为使用 DIR 命令获取 C: 上文件的内容,我希望将其输出放置在 SQL 服务器或访问 table。
我知道 xp_cmdshell 是此类命令的更好选择,但是我们的环境已禁用此功能,没有机会使用它。您可以提供的任何帮助将不胜感激。
谢谢。
您可以使用 FileSystemObject 获取您需要的有关 folders/files 的所有信息。然后,您可以查看文件夹中的所有文件和文件夹中的子文件夹,以填充内存中的 ADO 记录集。将此记录集设置为窗体的记录集。
Public Function GetFolderContents(path As String) As ADODB.Recordset
Dim fso As New FileSystemObject
Dim folderRecordSet As ADODB.Recordset
If Not fso.FolderExists(path) Then
Set GetFolderContents = Nothing
Exit Function
End If
Dim fol As Folder, fil As File, subFol As Folder
Set fol = fso.GetFolder(path)
Set folderRecordSet = GetNewFolderRecordset
For Each fil In fol.Files
If fil.Type <> "System file" Then
folderRecordSet.AddNew
folderRecordSet("Name") = fil.Name
folderRecordSet("Size") = fil.Size
folderRecordSet("Date modified") = fil.DateLastModified
folderRecordSet("Type") = fil.Type
folderRecordSet.Update
End If
Next fil
For Each subFol In fol.SubFolders
folderRecordSet.AddNew
folderRecordSet("Name") = subFol.Name
folderRecordSet("Size") = null
folderRecordSet("Date modified") = subFol.DateLastModified
folderRecordSet("Type") = subFol.Type
folderRecordSet.Update
Next subFol
Set GetFolderContents = folderRecordSet
End Function
Function GetNewFolderRecordset() As ADODB.Recordset
Set GetNewFolderRecordset = New ADODB.Recordset
With GetNewFolderRecordset
.Fields.Append "Name", adVarWChar, 255
.Fields.Append "Size", adInteger, , adFldIsNullable
.Fields.Append "Date modified", adDate
.Fields.Append "Type", adVarWChar, 255
.CursorType = adOpenKeyset
.CursorLocation = adUseClient
.LockType = adLockPessimistic
.Open
End With
End Function
然后在您的公开赛中,您可以将它们整合在一起。此代码段只是要列出您的临时目录内容。
Private Sub Form_Open(Cancel As Integer)
Set Me.Recordset = Module1.GetFolderContents(Environ("temp"))
End Sub
表单的控件必须绑定到记录集的列,如下所示
当您 运行 它在数据表视图中时,它看起来像这样
您可以捕获适当的 OnClick 事件来打开文件,或者如果 Type = "File Folder"
那么您可以重置记录集以更深一层。您也可以添加人为的 ..
以提高一级。或者制作自己的界面。如果您需要更多,File
和 Folder
对象中还有许多其他 file/folder 属性。