Reputation: 199
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"
Upvotes: 18
Views: 84305
Reputation: 31
This code will create a folder named with current date & time and copies the full content from "D:\Tally Data" to "08-10-2016 17 23" folder.
It requires a folder named "Tally Data" in your computer's D drive.(copy below code to a text document and save as a DOS batch file)
for /f "tokens=1* delims=" %%a in ('date /T') do set datestr=%%a
set time=%TIME:~0,2%" "%TIME:~3,2%
mkdir C:\%date:/=%" "%time%\Backup
xcopy "D:\Tally Data" C:\%date:/=%" "%time%\Backup /E /S /Q /Y
Upvotes: 2
Reputation: 81
You can set current date and time by doing this. I am using this daily in my batch file.
%date:~10%%date:~4,2%%date:~7,2%%time:~0,2%%time:~3,2%
ouput :
201509141639 ( 14th sept 2015 04:39 PM )
Upvotes: 8
Reputation: 1
setlocal
set "time=%time::=%"
rem check that date has / delimeter if not replace it for valid
md %date:/=%_%time:~0,-3%
endlocal
Upvotes: 0
Reputation: 41307
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
Upvotes: 26
Reputation: 70971
rem replace : with .
set myTime=%time::=.%
rem remove cents of second
set myTime=%myTime:~0,-3%
Upvotes: 7
Reputation: 161
http://www.dostips.com/DtTipsStringManipulation.php
Run a search for "Replace a substring."
Upvotes: 1