VBA - 以当前系统时间(年、月、日、时、分、秒)命名文件夹

时间:2025-02-07 14:43:22

VBA以当前系统时间(年、月、日、时、分、秒)命名文件夹

Sub addFile()
    Dim resFolderChild, nowTime, afterPath
    '获取系统当前时间并格式化,nowTime 就是自己想要的文件夹的名字,长这样:20191121173628
    nowTime = Format(Now, "yyyy" & "mm" & "dd" & "hh" & "mm" & "ss")
    'afterPath 是新建文件夹的路径,如果文件夹创建成功,那么它的路径就是afterPath
    afterPath = "D:\changeAfter_Files\" & nowTime
    '判断当前路径下是否已存在名为nowTime的文件夹(也就是直接判断新建文件夹的路径是否可以查找到,如果没有查找到,就说明在这个目录下,名为nowTime的文件夹不存在,那么就可以新建文件夹了)
    resFolderChild = Dir(afterPath, vbDirectory)
    If resFolderChild = "" Then
        MkDir (afterPath)
    End If
End Sub