在VB中如何以时间来创建目录
如题:如何以时间来创建目录(在VB中)?谢谢。
[解决办法]
需要呼叫api
Private Declare Function CreateDirectoryEx Lib "kernel32" Alias "CreateDirectoryExA" (ByVal lpTemplateDirectory As String, ByVal lpNewDirectory As String, lpSecurityAttributes As Any) As LongPrivate Sub Command1_Click() Dim DirPath, DirName DirPath = App.Path DirPath = IIf(Right(DirPath, 1) <> "\", DirPath = DirPath & "\", DirPath) '得到月份,月份小於10自動加0 V_month = IIf(Month(Date) < 10, "0" & Month(Date), Month(Date)) '得到日期,日期小於10自動加0 V_day = IIf(Day(Date) < 10, "0" & Day(Date), Day(Date)) V_hour = Hour(Time) '得到分鐘,分種小於10自動加0 V_min = IIf(Minute(Time) < 10, "0" & Minute(Time), Minute(Time)) '得到秒種,秒種小於10自動加0 V_sec = IIf(Second(Time) < 10, "0" & Second(Time), Second(Time)) '合併 DirName = Year(Date) & V_month & V_day & V_hour & V_min & V_sec Debug.Print DirName If CreateDirectoryEx("c:\windows", DirPath & DirName, ByVal 0&) = 0 Then MsgBox "False Created Directory" Exit Sub End IfEnd Sub
[解决办法]
引用FSO
dim FolderName as string
Set Fso = CreateObject("Scripting.FileSystemObject")
'设置文件夹名称,以时间命名
FolderName = Year(Date) & Month(Date) & Day(Date) & Hour(Time) & Minute(Time) & Second(Time)
'创建文件夹
Fso.CreateFolder ("D\" & FolderName)
完成
[解决办法]
Private Sub Command1_Click()
Dim strpath As String
strpath = Now
strpath = Replace(Now, ":", ":")
MkDir "d:\" & strpath
End Sub