【发布时间】:2018-10-01 08:22:54
【问题描述】:
我的代码有一个大问题,我是使用已知/某人的解决方案创建的。
发生了什么: 我需要一个代码来提取所有文件,其中 lastDateModified 早于某个特定日期。但是最好的解决方案是如果我将在数组中收到这些文件名(不知道该怎么做”
问题: 当我在控制台中输入命令时,它会正确地为我提供文件列表。
而当我将它放在 Access 中时,它会得到我的文本:
Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。
I:\Documents\Access>
代码:
Public Sub TestCommandLine()
Const lngCancelled_c As Long = 0
Dim strCmd As String
strCmd = "cmd.exe forfiles /P directory /S /D +01/04/2015) > directory2"
CommandLine strCmd, False
End Sub
Public Function CommandLine(command As String, Optional ByVal keepAlive As _
Boolean = False, Optional windowState As VbAppWinStyle =
VbAppWinStyle.vbHide) _
As Boolean
'--------------------------------------------------------------------------------
' Procedure : CommandLine
' Author : Aaron Bush (Oorang)
' Date : 10/02/2007
' Purpose : Provides a simple interface to execute a command lines from VBA.
' Input(s) :
' command : The DOS command you wish to execute.
' keepAlive : Keeps the DOS window open *after* command has been
' executed. Default behavior is to auto-close. (See
' remarks section for additional information.)
' windowState : Determines the window state of the DOS prompt
' *during* command execution.
' Output : True if completed with no errors, False if error encountered.
' Remarks : If the windowState property is set to vbHide while the keepAlive
' parameter is set to True, then windowState will be changed to
' vbNormalFocus.
'--------------------------------------------------------------------------------
On Error GoTo Err_Hnd
Const lngMatch_c As Long = 0
Const strCMD_c As String = "cmd.exe"
Const strComSpec_c As String = "COMSPEC"
Const strTerminate_c As String = " /c "
Const strKeepAlive_c As String = " /k "
Dim strCmdPath As String
Dim strCmdSwtch As String
If keepAlive Then
If windowState = vbHide Then
windowState = vbNormalFocus
End If
strCmdSwtch = strKeepAlive_c
Else
strCmdSwtch = strTerminate_c
End If
strCmdPath = VBA.Environ$(strComSpec_c)
If VBA.StrComp(VBA.Right$(strCmdPath, 7), strCMD_c, vbTextCompare) <> _
lngMatch_c Then
strCmdSwtch = vbNullString
End If
VBA.Shell strCmdPath & strCmdSwtch & command, windowState
CommandLine = True
VBA.Shell Nothing
Exit Function
Err_Hnd:
CommandLine = False
End Function
有人有这个问题吗?
【问题讨论】:
-
你在这里做了一些奇怪的事情。您正在使用的
CommandLine函数使用 shell 打开命令窗口。如果我正确阅读了您尝试的代码,则说明您正在使用 shell 打开命令窗口,然后在该命令窗口中使用cmd打开另一个命令窗口。这必然会引起怪异。
标签: vba shell ms-access cmd console