?
Solved

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

Posted on 2013-01-08
9
Medium Priority
?
402 Views
Last Modified: 2013-01-08
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
0
Comment
Question by:afflik1923
  • 4
  • 3
  • 2
9 Comments
 
LVL 44

Expert Comment

by:Amit
ID: 38754895
0
 

Author Comment

by:afflik1923
ID: 38755216
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"
0
 
LVL 44

Expert Comment

by:Amit
ID: 38755348
You need to change the regional setting. As date and time format is picked up from regional setting of OS.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

Expert Comment

by:oBdA
ID: 38755361
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

0
 

Author Comment

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

thanks
0
 
LVL 44

Expert Comment

by:Amit
ID: 38755382
oBdA, i tested your script and it allows to create dir one time and next time it gives dir already exist error.
0
 

Author Comment

by:afflik1923
ID: 38755396
yeah i just need to add the time on the end
0
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 38755406
@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%_%Hour%%Minute%%Second%
echo Time stamp: %TimeStamp%
n:
if not exist "%1\%TimeStamp%" md "%1\%TimeStamp%"

Open in new window

0
 

Author Comment

by:afflik1923
ID: 38755547
Ah that's perfect oBdA, that's exactly what i needed.

Thanks for the quick responses.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question