Avatar of afflik1923
afflik1923Flag for United Kingdom of Great Britain and Northern Ireland

asked on 

Script to create directory named with today's date and time

I'm looking to set up a script to copy some files, i was just going to run a simple batch file that runs robocopy. I wish the script to create a directory with the current date and time  before it copies the files.

I've done a bit of googling and have come across the following however it doesn't seem to work

FOR /F “TOKENS=1,2 DELIMS=/ ” %%A IN (‘DATE /T’) DO SET mm=%%B
FOR /F “TOKENS=2,3 DELIMS=/ ” %%A IN (‘DATE /T’) DO SET dd=%%B
FOR /F “TOKENS=3* DELIMS=/ ” %%A IN (‘DATE /T’) DO SET yyyy=%%B

set /A dd=%dd%

set Pdate=%yyyy%_%mm%_%dd%
echo %Pdate%

mkdir C:MYFILE_”%PDATE%


It just gives the error: 1* was unexpected at this time

Any thoughts?

Thanks
Windows Batch

Avatar of undefined
Last Comment
afflik1923
Avatar of Amit
Amit
Flag of India image

Avatar of afflik1923
afflik1923
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

ok so i tried the following:

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
)

set date = %Year%%Month%%Day%

n:
md "%1\%Year%%Month%%Day%"

However the date comes out as 08/01/2013 which creates sub folders of "08" and "01" and "2013" rather than "08012013"
Avatar of Amit
Amit
Flag of India image

You need to change the regional setting. As date and time format is picked up from regional setting of OS.
Avatar of oBdA
oBdA

You'd need to
1. use "/ " instead of ". " as delimiters for the date output,
2. rearrange the tokens in the date for loop in all likelihood,
3. stop using spaces around the equal sign when assigning variables,
4. avoid overwriting predefined variables ("date")
to correct your script.
Or you could avoid all those nasty problems with localization and regional settings by using WMI to query for the date and time. It's a bit more complicated, but does not rely on specific date and time settings and works from Windows 2000 until at least Windows 7/W2k8R2:
@echo off
setlocal enabledelayedexpansion
set /a Line=0
for /f "tokens=1-9" %%a in ('wmic Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year ^| find /v ""') do (
REM for /f "skip=1 tokens=2-10 delims=," %%a in ('wmic.exe Path Win32_LocalTime Get Day^,DayOfWeek^,Hour^,Minute^,Month^,Quarter^,Second^,WeekInMonth^,Year /format:csv') do (
  set /a Line += 1
  if "!Line!"=="1" (set VarA=%%a&set VarB=%%b&set VarC=%%c&set VarD=%%d&set VarE=%%e&set VarF=%%f&set VarG=%%g&set VarH=%%h&set VarI=%%i)
  if "!Line!"=="2" (set !VarA!=%%a&set !VarB!=%%b&set !VarC!=%%c&set !VarD!=%%d&set !VarE!=%%e&set !VarF!=%%f&set !VarG!=%%g&set !VarH!=%%h&set !VarI!=%%i)
)
for %%a in (Month Day Hour Minute Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
REM *** At this point, the variables Day, DayOfWeek, Hour, Minute, Month, Quarter, Second, WeekInMonth, and Year are set.
REM *** Month, Day, Hour, Minute, Second have leading zeroes if less than 10.
set TimeStamp=%Year%%Month%%Day%
echo Time stamp: %TimeStamp%
n:
md "%1\%TimeStamp%"

Open in new window

Avatar of afflik1923
afflik1923
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

ah right, i just changed the short date format to be 08-01-2013 and it works now

thanks
Avatar of Amit
Amit
Flag of India image

oBdA, i tested your script and it allows to create dir one time and next time it gives dir already exist error.
Avatar of afflik1923
afflik1923
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

yeah i just need to add the time on the end
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of afflik1923
afflik1923
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Ah that's perfect oBdA, that's exactly what i needed.

Thanks for the quick responses.
Windows Batch
Windows Batch

Batch files are text files containing a script of commands that are executed by the command interpreter on DOS, OS/2 and Windows systems. Most commonly, they are used to perform a series of functions that are repeated -- copying a set of files created daily with one step, for example.

13K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo