Sludgedog
Sludgedog

Reputation: 391

How do i make a batch file with a time and date as a name?

I want to make a batch file that makes backups of a specific folder and so i want the date and time as a name

code::

for /f "tokens=1-3 delims=:" %%a in ("%time%") do md "%%a.%%b.%%c"

for /f "tokens=1-3 delims=/" %%a in ("%date%") do md "%%a_%%b_%%c"

md "(%date%%time%)"

This makes two separate folders where i only want one.

Upvotes: 0

Views: 840

Answers (2)

Endoro
Endoro

Reputation: 37569

try this:

md "%date:/=-%-%time::=-%"

Upvotes: 1

foxidrive
foxidrive

Reputation: 41234

This is a more robust and reliable method - XP Pro and higher:

@echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
set "YY=%dt:~2,2%"
set "YYYY=%dt:~0,4%"
set "MM=%dt:~4,2%"
set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%"
set "Min=%dt:~10,2%"
set "Sec=%dt:~12,2%"

MD "%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"

Upvotes: 2

Related Questions