I need to write a bat file which creates a new folder using current date and time for folder name. I came up with the following:
for /f "tokens=1-3 delims=:," %%i in ("%TIME%") do md %DATE%-%%i.%%j.%%k
Does this code has any flaws? Is there an easier / more natural way to do it?
Run batch file with Task Scheduler Open Start. Search for Task Scheduler and click the top result to open the app. Right-click the "Task Scheduler Library" branch and select the New Folder option. Confirm a name for the folder — for example, MyScripts.
Introduction# One useful feature of batch files is being able to create files with them.
Here is how you can do that: Type Notepad in Windows search and click Open. In the Notepad window, click type @ECHO OFF and click Enter. After you have typed down the names of all the folders and subfolders that you want to create, navigate to File in the top-left corner and choose Save as.
You can use a substring and the built-in %DATE% and %TIME% variables to do this:
@echo OFF
:: Use date /t and time /t from the command line to get the format of your date and
:: time; change the substring below as needed.
:: This will create a timestamp like yyyy-mm-dd-hh-mm-ss.
set TIMESTAMP=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
@echo TIMESTAMP=%TIMESTAMP%
:: Create a new directory
md "%1\%TIMESTAMP%"
I use this bat
for /F "tokens=1-4 delims=. " %%i in ('date /t') do (
set Day=%%i
set Month=%%j
set Year=%%k
)
for /F "tokens=1-4 delims=: " %%i in ('time /t') do (
set Hour=%%i
set Minute=%%j
set Second=%%k
)
md %1\%Year%-%Month%-%Day%
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With