Batch to create multi folders in date range

April 9, 2011 at 00:50:49
Specs: Windows XP
Hi,

I've been search for 2 hrs on this and haven't found what I'm looking for. I want to have a batch when executed will prompt the user for a date range and create folders for each day within that range. So for example, I can input 20110409 and 20110430 and a folders are created named 20110409, 20110410.....20110430.

Any recommendations would be helpful.

Thanks


See More: Batch to create multi folders in date range

Report •


#1
April 9, 2011 at 02:21:03
Batch scripting is less than efficient in date handling, in fact it could be described as hopeless. You should look at a different scripting language such as VBS to set the date info you want. If you wish to continue please enter echo %date% at the command prompt and post the result.

Did you Google for an answer before asking the question?


Report •

#2
April 9, 2011 at 02:59:00
Ok I just wrote this so I can't guarantee it's accuracy, provided as is.

It echo's "md yyyymmdd", you can remove the echo after
checking(marked towards the end of the script).

Edit: I changed the script to check start <= end.

 
@echo off
SetLocal EnableDelayedExpansion

rem ************************************************************
rem ****************** INPUT AND VALIDATION ********************
rem ************************************************************
set daterange=
:input
if defined daterange Echo input error - check dates
set daterange=
set /p daterange=Input a daterange in the format "yyyymmdd-yyyymmdd"
if not defined daterange goto input
for /f "tokens=1 delims=-0123456789 " %%a in ("%daterange:"=%") do goto input
for /f "tokens=1,2 delims=- " %%a in ("%daterange:"=%") do (
    for /f "tokens=* delims=0123456789" %%c in ("%%a%%b") do if not "%%c"=="" goto input
    set startdate=%%a
    set enddate=%%b
)
if "%startdate:~7%"=="" goto input
if not "%startdate:~8%"=="" goto input
if "%enddate:~7%"=="" goto input
if not "%enddate:~8%"=="" goto input

set startyear=%startdate:~0,4%
set startmon=%startdate:~4,2%
set startday=%startdate:~6%
set endyear=%enddate:~0,4%
set endmon=%enddate:~4,2%
set endday=%enddate:~6%
if "%startday:~0,1%"=="0" set startday=%startday:~1%
if "%startmon:~0,1%"=="0" set startmon=%startmon:~1%
if "%endday:~0,1%"=="0" set endday=%endday:~1%
if "%endmon:~0,1%"=="0" set endmon=%endmon:~1%
for %%a in (%startmon% %startday% %endmon% %endday%) do if %%a==0 goto input

if %startyear% gtr %endyear% goto input
if %startyear% equ %endyear% if %startmon% gtr %endmon% goto input
if %startyear% equ %endyear% if %startmon% equ %endmon% (
    if %startday% gtr %endday% goto input
)

for %%a in (1 3 5 7 8 10 12) do (
    if %startmon%==%%a if %startday% gtr 31 goto input
    if %endmon%==%%a if %endday% gtr 31 goto input
)
for %%a in (4 6 9 11) do (
    if %startmon%==%%a if %startday% gtr 30 goto input
    if %endmon%==%%a if %endday% gtr 30 goto input
)
if '!!==' (set esc=^^) else set esc=
set /a leap="(%esc%!(%startyear%00 %% 400)) & (%esc%!(%startyear%00 %% 100)) * (%esc%!(%startyear% %% 4))"
if %startmon%==2 (
    if %leap%==1 (
        if %startday% gtr 29 goto input
    ) else if %startday% gtr 28 goto input
)
set /a leap="(%esc%!(%endyear%00 %% 400)) & (%esc%!(%endyear%00 %% 100)) * (%esc%!(%endyear% %% 4))"
if %endmon%==2 (
    if %leap%==1 (
        if %endday% gtr 29 goto input
    ) else if %endday% gtr 28 goto input
)
rem ************************************************************
rem ***************** END INPUT AND VALIDATION *****************
rem ************************************************************


set emon=12
set smon=%startmon%
set sday=%startday%
for /l %%a in (%startyear% 1 %endyear%) do (
    if %%a==%endyear% set emon=%endmon%
    for /l %%b in (!smon! 1 !emon!) do (
        for %%d in (1 3 5 7 8 10 12) do if %%b==%%d set eday=31
        for %%e in (4 6 9 11) do if %%b==%%e set eday=30
        if %%b==2 (
            set /a leap="(%esc%!(%%a00 %% 400)) & (%esc%!(%%a00 %% 100)) * (%esc%!(%%a %% 4))"
            if !leap!==1 (set eday=29) else set eday=28
        )
        if %%a==%endyear% if %%b==%endmon% set eday=%endday%
        for /l %%c in (!sday! 1 !eday!) do (
            set mon=0%%b
            set mon=!mon:~-2!
            set day=0%%c
            set day=!day:~-2!
rem ********************************************************
rem ******************* The actual output ******************
            echo md %%a!mon!!day!
rem ********************************************************
rem ********************************************************
        )
        set sday=1
    )
    set smon=1
)
pause


Report •

#3
April 9, 2011 at 04:03:21
Judago, It worked flawless. Many thanks mate!!! When I started I tried the naming as a numerical sequence but with yours the date works through multiple months vs just one month.

Report •

Related Solutions

#4
April 9, 2011 at 04:41:21
No worries, let me know if you find any problems.

Report •

#5
April 9, 2011 at 06:02:02
Just letting you know that i found that it wasn't calculating leap
years 100% correct. You probably didn't notice it because
the previous year that was incorrect was 1900 and the next is 2100.

So here is it fixed:

@echo off
SetLocal EnableDelayedExpansion

rem ************************************************************
rem ****************** INPUT AND VALIDATION ********************
rem ************************************************************
set daterange=
:input
if defined daterange Echo input error - check dates
set daterange=
set /p daterange=Input a daterange in the format "yyyymmdd-yyyymmdd"
if not defined daterange goto input
for /f "tokens=1 delims=-0123456789 " %%a in ("%daterange:"=%") do goto input
for /f "tokens=1,2 delims=- " %%a in ("%daterange:"=%") do (
    for /f "tokens=* delims=0123456789" %%c in ("%%a%%b") do if not "%%c"=="" goto input
    set startdate=%%a
    set enddate=%%b
)
if "%startdate:~7%"=="" goto input
if not "%startdate:~8%"=="" goto input
if "%enddate:~7%"=="" goto input
if not "%enddate:~8%"=="" goto input

set startyear=%startdate:~0,4%
set startmon=%startdate:~4,2%
set startday=%startdate:~6%
set endyear=%enddate:~0,4%
set endmon=%enddate:~4,2%
set endday=%enddate:~6%
if "%startday:~0,1%"=="0" set startday=%startday:~1%
if "%startmon:~0,1%"=="0" set startmon=%startmon:~1%
if "%endday:~0,1%"=="0" set endday=%endday:~1%
if "%endmon:~0,1%"=="0" set endmon=%endmon:~1%
for %%a in (%startmon% %startday% %endmon% %endday%) do if %%a==0 goto input

if %startyear% gtr %endyear% goto input
if %startyear% equ %endyear% if %startmon% gtr %endmon% goto input
if %startyear% equ %endyear% if %startmon% equ %endmon% (
    if %startday% gtr %endday% goto input
)

for %%a in (1 3 5 7 8 10 12) do (
    if %startmon%==%%a if %startday% gtr 31 goto input
    if %endmon%==%%a if %endday% gtr 31 goto input
)
for %%a in (4 6 9 11) do (
    if %startmon%==%%a if %startday% gtr 30 goto input
    if %endmon%==%%a if %endday% gtr 30 goto input
)
if '!!==' (set esc=^^) else set esc=
set /a leap="(%esc%!(%startyear% %% 100) * -1) + %esc%!(%startyear% %% 400) + %esc%!(%startyear% %% 4)"
if %startmon%==2 (
    if %leap%==1 (
        if %startday% gtr 29 goto input
    ) else if %startday% gtr 28 goto input
)
set /a leap="(%esc%!(%endyear% %% 100) * -1) + %esc%!(%endyear% %% 400) + %esc%!(%endyear% %% 4)"
if %endmon%==2 (
    if %leap%==1 (
        if %endday% gtr 29 goto input
    ) else if %endday% gtr 28 goto input
)
rem ************************************************************
rem ***************** END INPUT AND VALIDATION *****************
rem ************************************************************


set emon=12
set smon=%startmon%
set sday=%startday%
for /l %%a in (%startyear% 1 %endyear%) do (
    if %%a==%endyear% set emon=%endmon%
    for /l %%b in (!smon! 1 !emon!) do (
        for %%d in (1 3 5 7 8 10 12) do if %%b==%%d set eday=31
        for %%e in (4 6 9 11) do if %%b==%%e set eday=30
        if %%b==2 (
            set /a leap="(%esc%!(%%a %% 100) * -1) + %esc%!(%%a %% 400) + %esc%!(%%a %% 4)"
            if !leap!==1 (set eday=29) else set eday=28
        )
        if %%a==%endyear% if %%b==%endmon% set eday=%endday%
        for /l %%c in (!sday! 1 !eday!) do (
            set mon=0%%b
            set mon=!mon:~-2!
            set day=0%%c
            set day=!day:~-2!
rem ********************************************************
rem ******************* The actual output ******************
            echo md %%a!mon!!day!
rem ********************************************************
rem ********************************************************
        )
        set sday=1
    )
    set smon=1
)
pause


Report •

#6
April 9, 2011 at 12:57:50
Judago, thanks again. That completely slipped my mind. I do have another question. Now I'm trying to run it from a share folder but since the share is UNC its needs a defined letter: path.

I tried
pushd \\server\sharename

but no luck. Would you happen to know how to run the batch from any share folder that it is in, with out changing the reg?

Thanks.


Report •

#7
April 9, 2011 at 16:32:32
I'm not sure, I don't deal with networks very often...

Maybe someone else can help with that one ;)


Report •


Ask Question