概要
关键点:获取日期、格式转换,循环递归、创建文件夹
- 获取系统日期并进行适当的格式转换
- 根据需求创建一个循环体(这里是取当前日期的前后N天)
- 递归创建文件夹
代码
提示:Option Explicit会导致For循环内代码失效,暂未考证原因
- createobject(“Scripting.FileSystemObject”).GetFolder(“.”).Path的作用是获取当前执行脚本的路径
'Option Explicit
Dim iDate
Dim ePath : ePath = createobject("Scripting.FileSystemObject").GetFolder(".").Path
Dim sPath
Dim iStep
iStep = InputBox("The folder will be created according to the current date plus or minus the specified number of days.If no input is made, the folder will be created according to the current date.","Enter days")
if isNumeric(iStep) = false then iStep = 0 end if
For i = (-1 * iStep) To iStep
iDate = DateAdd("d",i,date)
iDate = Right("0000" & Year(iDate), 4) & Right("00" & Month(iDate), 2) & Right("00" & Day(iDate), 2)
sPath = ePath & "\" & iDate
Call CreateFolder(sPath)
Next
Function CreateFolder(strFolder)
Dim objFSO, strParentFolder, strSubFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FolderExists(strFolder) Then
strParentFolder = objFSO.GetParentFolderName(strFolder)
If Not objFSO.FolderExists(strParentFolder) Then
Call CreateFolder(strParentFolder)
End If
objFSO.CreateFolder(strFolder)
End If
End Function