Yarik
Yarik

Reputation: 1202

Creating folder using bat 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?

Upvotes: 16

Views: 86384

Answers (8)

Patrick Cuff
Patrick Cuff

Reputation: 29786

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%"

Upvotes: 25

Rune Pedersen
Rune Pedersen

Reputation: 11

The reason why a simple %time% does not work is because its separated by : which is not allowed in batch scripting. But this one should work fine:

It works very well for me.

Rem Get Day,Mth & Year from %Date%
set Day=%Date:~0,2%
set Mth=%Date:~3,2%
set Yr=%Date:~6,4%
REM Get Hour and Min from %Time%
set Hour=%Time:~0,2%
if "%hour:~0,1%" == " " set hour=0%hour:~1,1%
set Min=%Time:~3,2%

mkdir %date%-%hour%.%min%

Upvotes: 1

ryan vigus
ryan vigus

Reputation: 1

this works pretty well for me

dkdir foldername

cd foldername

dkdir %date% %time% 

cls

Upvotes: 0

Be Brave Be Like Ukraine
Be Brave Be Like Ukraine

Reputation: 7735

You may take advantage of GNU sh-utils using date.exe's format specifiers:

for /f "delims=" %%a in ('date.exe +%%Y-%%m-%%d.%%H-%%M') do @set dd=%%a
md %dd%

This solution is especially important for those with localized versions of Windows.

for loop runs once, but it is needed since there's no simpler way to assign a variable with the the value taken from command's output.

Use date --help for the full list of format specifiers.

Upvotes: 0

mhenry1384
mhenry1384

Reputation: 7678

Here's my solution (modified off lopkiju's). It creates a folder using a datestamp. If a folder by that name already exists, append an integer to it to make it unique. Put this in a .bat file and pass in the path to the parent folder.
Tested in Windows 7 Home Premium x64.

REM Create a folder using the current date.  If such a folder already exists, append a number to make it unique.
@ECHO OFF
for /F "tokens=2-5 delims=/ " %%i in ('date /t') do (
set Day=%%j
set Month=%%i
set Year=%%k
)

set Path=%~1\%Year%-%Month%-%Day%
set /a inx=2

:LOOP
IF NOT EXIST "%Path%" GOTO CREATE
set Path=%~1\%Year%-%Month%-%Day% (%inx%)
set /a inx+=1
if %inx% gtr 9 goto :END REM Eh, giving up.
goto LOOP
:END

:CREATE
md "%Path%"
:END

Upvotes: 0

Alexander Angell
Alexander Angell

Reputation: 1

have you not just tried to make a new new folder maker using date and time saved as new_folder_maker.bat with this code

:start
@echo off
md new_folder_%date%_%time%
exit

Upvotes: 0

HaydnWVN
HaydnWVN

Reputation: 11

I couldn't get many of the above to work, used the original posters command successfully though.

Here's my version:

for /f "tokens=1-3 delims=:/" %%i in ("%DATE%") do set DATESTAMP=%%i.%%j.%%k
@echo Directory will be made with this name:
@echo backup.%DATESTAMP%
md backup.%DATESTAMP%
@echo Directory made, now proceeding with file copy...
@echo please ensure PC doesn't have E-mail or any documents open.
pause
cd backup.%DATESTAMP%
md "Shared Documents"
xcopy "c:\documents and settings\all users\documents\documents" "Shared Documents" /E /C /I /H /Q
@echo in Documents Complete.

etc :)

Upvotes: 1

Codesmell
Codesmell

Reputation: 379

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.

Upvotes: 6

Related Questions