Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-01-08
9
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

618 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