Batch Script to Create Directory Based on Date

Someone needed a script to create a directory based on today’s date in batch. However, he also needed to be able to create multiple directories in one day without overwriting previous ones. Also, he didn’t want it to be creating temp files and calling other batch files and so on. It needed to be simple.

 

Here’s my solution:

@echo off
for /f "usebackq tokens=1-3" %%a in (`date /t`) do (
    for /f "delims=/ tokens=1-3" %%i in ("%%b") do (
        for /l %%m in (1,1,50) do (
            if exist %%i-%%j-%%k-%%m (
                rem do nothing
            ) else (
                mkdir %%i-%%j-%%k-%%m
                cd %%i-%%j-%%k-%%m
                goto finis
            )
        )
    )
)
:finis
rem do stuff in our directory here like copy some files
rem copy c:\temp\*.* .



Back to Article Index

blog comments powered by Disqus