【VBS】按日期范围创建文件夹

文章目录

概要

关键点:获取日期、格式转换,循环递归、创建文件夹

  1. 获取系统日期并进行适当的格式转换
  2. 根据需求创建一个循环体(这里是取当前日期的前后N天)
  3. 递归创建文件夹

代码

提示:Option Explicit会导致For循环内代码失效,暂未考证原因

  1. 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
  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值