Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating folder using bat file

Tags:

batch-file

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?

like image 990
Yarik Avatar asked Feb 22 '09 11:02

Yarik


People also ask

How do I create a .bat folder?

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.

Can a batch file create files?

Introduction# One useful feature of batch files is being able to create files with them.

How do you create multiple folders at once?

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.


2 Answers

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%"
like image 189
Patrick Cuff Avatar answered Sep 28 '22 06:09

Patrick Cuff


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.

like image 43
Codesmell Avatar answered Sep 28 '22 07:09

Codesmell