[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3957
  • Last Modified:

MKDIR YEAR/Month/Day

Id like to create a set of folders like

2007/7/20

I need to do this a a batch file, I can do 'mkdir %date%' but creates the details wrong, is it possible to do 'mkdir %year%' to create  a folder named '2007?

Tony
0
tonelm54
Asked:
tonelm54
  • 5
  • 3
1 Solution
 
Christopher McKayCommented:
The easiest way to accomplish this would be:
to have this in your batch file:

for /f "tokens=3,4 delims=/" %%a in ('date /t') do (
      mkdir  %%a
      )

Hope this helps!

:o)

Bartender_1
0
 
tonelm54Author Commented:
how would I do month and day?
0
 
Christopher McKayCommented:
Actually this method may work better for you....

use this command instead:

mkdir %date:~10,4%

(This assumes that when you type "echo %date%" at the command prompt your result is in this format:

Fri 07/20/2007

If your result is not in this format, then let me know what the result is, and I'll taylor the command to match it.

:o)

Bartender_1
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tonelm54Author Commented:
Im wondering why this does not work!

------------------------------------------------------------------------------------
for /f "tokens=1,2 delims=/" %%d in ('date /t') do (
      echo  %%d
      )
for /f "tokens=3,4 delims=/" %%m in ('date /t') do (
      echo  %%m
      )
for /f "tokens=4,5 delims=/" %%y in ('date /t') do (
      echo  %%y
      )
mkdir %%y
cd %%y
mkdir %%m
cd %%m
mkdir %%d
cd %%d
0
 
Christopher McKayCommented:
If you use the second method,  then you could do this in your batch file:

mkdir %date:~10,4%\%date:~4,2%\%date:~7,2%

:o)

Bartender_1
0
 
tonelm54Author Commented:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\user\Desktop>mkdir %date:~10,4%\%date:~4,2%\%date:~7
,2%
The syntax of the command is incorrect.

C:\Documents and Settings\user\Desktop>test.cmd

C:\Documents and Settings\user\Desktop>mkdir \7/\00
The syntax of the command is incorrect.
0
 
Christopher McKayCommented:
That means your date format is different than mine,

Can you post the results of typing this at the command prompt:

echo %date%
0
 
Christopher McKayCommented:
What's happening with the command I'm using is this:

%date% spits out a result in whatever format you see when you type "echo %date%"
The ":~#,#% tell the computer to skip the number of characters after the "~" and display the number of characters after the ",".

So, in this instance: %date:~10,4%
it's being told to look at the date, and display the first 4 characters after skipping the first 10.

So in my date format, it skips

Fri 07/20/

and reads the next 4 characters, which are:

2007

Using that logic, it shouldn't be hard to figure out what you need to be able to get the command right for you.

:o)

Bartender_1
0
 
SteveGTRCommented:
You can't use slashes in the directory names. You might try underscores. Here's some code that will work. It doesn't care what the format you are using for the date. I've disabled the mkdir command and simply echo out the command so no directory is created. To enable the command remove the echo command.

@echo off

setlocal

call :GETDATEPARTS "%date%"

echo mkdir %yy%_%mm%_%dd%

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0

Featured Post

Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now