Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to create a folder based on current date & time

we have a simple batch file that creates a backup of a folder and appends the date & time to the end.

We use this incrementally and it outputs a folder such as "data 28-04-13".

I would like to add the time to the end of this, however my code outputs time as HH:MM, which is not valid for a folder name as it includes a colon (:).

Please could someone modify my code to remove the :, or replace it with a ".".

Thank You

@echo off & for /F "tokens=1-4 delims=/ " %%A in ('date/t') do (
set DateDay=%%A
set DateMonth=%%B
set DateYear=%%C
)

@echo off & for /F "tokens=1-4 delims=/ " %%D in ('time/t') do (
set DateTime=%%D
)

set CurrentDate=%DateDay%-%DateMonth%-%DateYear%-%DateTime%

md "F:\MobilePC\data %CurrentDate"

Answered my own question

So, this was the easiest way for me:

set CurrentDate=%DateDay%-%DateMonth%-%DateYear%-%time:~0,2%.%time:~3,2%

Which outputs "31-10-13-11.35"

like image 643
TheNineteenNineties Avatar asked Oct 31 '13 11:10

TheNineteenNineties


2 Answers

The first four lines of this code will give you reliable YY DD MM YYYY HH Min Sec variables in XP Pro and higher.

The built in cmd date and time variables are user configurable and so are unreliable for any general batch file.

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') 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%"

set "datestamp=%YYYY%%MM%%DD%" & set "timestamp=%HH%%Min%%Sec%"
set "fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
echo fullstamp: "%fullstamp%"
pause
like image 96
foxidrive Avatar answered Oct 19 '22 13:10

foxidrive


rem replace : with .
set myTime=%time::=.%

rem remove cents of second
set myTime=%myTime:~0,-3%
like image 35
MC ND Avatar answered Oct 19 '22 14:10

MC ND