1

I've been trying to create a basic batch script that will create folder for the current year (e.g. 2014) and then a sub-folder for the current month (June, etc...). Once these are established, I need to create folders based on the different domains in our forest (e.g. Homer, Janeway, Autobots domains).

When I type date, I get the following (it's the same on all our systems):

Mon 06/30/2014

Here is the code I have so far:


set yearfoldername=%date:~-4%
set month=%date:~-10,2%
set 01=January
set 02=February
set 03=March
set 04=April
set 05=May
set 06=June
set 07=July
set 08=August
set 09=September
set 10=October
set 11=November
set 12=December

md %yearfoldername%
md %yearfoldername%\%%month%%
md %yearfoldername%\%%month%%\Homer
md %yearfoldername%\%%month%%\Janeway
md %yearfoldername%\%%month%%\Autobots

As it stands, it only creates folders that look like this: 2014\%month%\Homer instead of 2014\June\Homer

John DOE
  • 400
  • 1
  • 3
  • 18
TrikerII
  • 9
  • 1
  • 2

5 Answers5

2

There is probally better ways but i belive this will work;

set yearfoldername=%date:~-4%
set month=%date:~-10,2%

if "%month%" EQU "01" set asd=January
if "%month%" EQU "02" set asd=February
if "%month%" EQU "03" set asd=March
if "%month%" EQU "04" set asd=April
if "%month%" EQU "05" set asd=May
if "%month%" EQU "06" set asd=June
if "%month%" EQU "07" set asd=July
if "%month%" EQU "08" set asd=August
if "%month%" EQU "09" set asd=September
if "%month%" EQU "10" set asd=October
if "%month%" EQU "11" set asd=November
if "%month%" EQU "12" set asd=December

md %yearfoldername%
md %yearfoldername%\%asd%
md %yearfoldername%\%asd%\Homer
md %yearfoldername%\%asd%\Janeway
md %yearfoldername%\%asd%\Autobots
John DOE
  • 400
  • 1
  • 3
  • 18
1

This is the way I would do it:

@echo off
setlocal EnableDelayedExpansion

rem Define the array of month names
set m=100
for %%a in (January February March     April   May      June
            July    August   September October November December) do (
   set /A m+=1
   set monthName[!m:~-2!]=%%a
)

for /F "tokens=2,4 delims=/ " %%a in ("%date%") do (
   set yearFolderName=%%b
   set month=%%a
)

md %yearFolderName%
md %yearFolderName%\!monthName[%month%]!
for %%a in (Homer Janeway Autobots) do (
   md %yearFolderName%\!monthName[%month%]!\%%a
)

For further details on array management, see this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • May i ask why `set m=100` started from 100 ? And why not `monthName[!m!]` instead of `monthName[!m:~-2!]` ? – John DOE Jun 30 '14 at 22:08
  • @JohnDOE: Because month numbers have a left zero: `01 02 ... 09 10 11 12` If `m` would started at zero, it would be necessary to insert a left zero in values between 1 and 9, or eliminate the left zero from month numbers. This way, it is very easy to set `m=101, 102, ...` and get the two last characters: `01 02...` – Aacini Jul 01 '14 at 01:49
1

Just another way

@echo off
    setlocal enableextensions disabledelayedexpansion

:: Data section : table of months

    ::month:01:January
    ::month:02:February
    ::month:03:March
    ::month:04:April
    ::month:05:May
    ::month:06:June
    ::month:07:July
    ::month:08:August
    ::month:09:September
    ::month:10:October
    ::month:11:November
    ::month:12:December

:: Code Section

    for /f "tokens=2,4 delims=/ " %%a in ( "%date%"
    ) do for /f "tokens=3 delims=: " %%c in ( 'findstr /l /c:"::month:%%a" "%~f0"'
    ) do for %%d in ( Homer Janeway Autobots
    ) do md "%%b\%%c\%%d"

    endlocal

It splits the date to retrieve the required fields, retrieve the field name from the Data section declared in the batch file (uses findstr to retrieve the correct line according to the month number) and for each of the domains the corresponding folder is created.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • 1
    +1 A different method! You define a lookup table of values and search it via `findstr`. There is an interesting trick that use `setx` command to perform a similar lookup table search, as described at [this post](http://www.dostips.com/forum/viewtopic.php?f=3&t=5083), for example: `...) for /f "tokens=3" %%c in ('setx /F "%~f0" dummyVar /D : /R 0^,1 %%a') do set "month=%%c" & for %%d in ( Homer Janeway Autobots ) do md "%%b\!month:~0,-1!\%%d"`. For a small test, type: `setx /F yourfile.bat /D : /X` – Aacini Jul 01 '14 at 09:43
  • @Aacini, i didn't remember the coordinate data extraction of setx! Thank you. – MC ND Jul 01 '14 at 09:58
0

Your original code had an extra set of unwanted percents. For example md %yearfoldername%\%%month%%\Homer should instead be md %yearfoldername%\%month%\Homer.

Note that the MD command can create multiple folders with one command. You don't need the following:

md %yearfoldername%
md %yearfoldername%\%month%

because the command below will create both of the above folders.

md %yearfoldername%\%month%\Homer

Your parsing of the DATE command could fail on some machines depending on the configuration, since date is locale dependent. It sounds like you are aware of this since you state "(it's the same on all our systems)".

I have written a hybrid JScript/batch utility called getTimestamp.bat that makes it extremely convenient to work with date and time on any Windows machine, regardless of locale settings. The utility is pure script that will run on any Windows machine from XP onward. Full documentation is embedded within the script.

As long as getTimestamp.bat is in your current directory, or better yet, somewhere within your PATH, then the following is all that is needed:

@echo off
call getTimestamp -f {yyyy}\{month} -r yrMo
for %%F in (Homer Janeway Autobots) do md "%yrMo%\%%F" 2>nul

I redirected error output to NUL just in case any of the folders already exist.

It is easy to adapt the script for any language. Here is the solution for Spanish:

@echo off
call getTimestamp -f {yyyy}\{month} -r yrMo -month "enero febrero marzo abril mayo junio julio agosto septiembre octubre noviembre diciembre"
for %%F in (Homer Janeway Autobots) do md "%yrMo%\%%F" 2>nul
dbenham
  • 127,446
  • 28
  • 251
  • 390
-1

Awesome! I saw the answers here and all would work great for me. I ended up fixing the issue right before I came back here to see the responses. Thank you so much everyone for your help. I'm still learning this stuff, so all responses show me different ways of doing things. Here's what I ended up doing (based on my limited knowledge, so code is long-winded):

@echo off
REM This batch file Will create a tree structure based on year, month and domain
set year=%date:~-4%
:Month-Name-Creation
if %date:~-10,2%==01 goto Jan
if %date:~-10,2%==02 goto Feb
if %date:~-10,2%==03 goto Mar
if %date:~-10,2%==04 goto Apr
if %date:~-10,2%==05 goto Maybe
if %date:~-10,2%==06 goto Jun
if %date:~-10,2%==07 goto Jul
if %date:~-10,2%==08 goto Aug
if %date:~-10,2%==09 goto Sep
if %date:~-10,2%==10 goto Oct
if %date:~-10,2%==11 goto Nov
if %date:~-10,2%==12 goto Dec

:Jan
md %year%\January
set month=January
goto Domains


:Feb
md %year%\February
set month=February
goto Domains

:Mar
md %year%\March
set month=March
goto Domains

:Apr
md %year%\April
set month=April
goto Domains

:Maybe
md %year%\May
set Month=May
goto Domains

:Jun
md %year%\June
set month=June
goto Domains

:Jul
md %year%\July
set month=July
goto Domains

:Aug
md %year%\August
set month=August
goto Domains

:Sep
md %year%\September
set month=September
goto Domains

:Oct
md %year%\October
set month=October
goto Domains

:Nov
md %year%\November
set month=November
goto Domains

:Dec
md %year%\December
set month=December
goto Domains

:Domains
cd %year%\%month%
md Homer Janeway Autobots

Again, thank you all for your responses.

/r

TrikerII

TrikerII
  • 9
  • 1
  • 2
  • 1
    Why is your May label called `:Maybe`? Also, you should accept an answer, there are plenty to choose from. :P – Dan Bechard Jul 01 '14 at 15:21