Link to home
Start Free TrialLog in
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
Avatar of Amit
Amit
Flag of India image

Avatar of afflik1923

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

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

thanks
oBdA, i tested your script and it allows to create dir one time and next time it gives dir already exist error.
yeah i just need to add the time on the end
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah that's perfect oBdA, that's exactly what i needed.

Thanks for the quick responses.