Solved

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

Posted on 2013-01-08
9
371 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 41

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 41

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
 
LVL 82

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 41

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 82

Accepted Solution

by:
oBdA earned 500 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now