Jump to content

Hey guys. I need to create a batch script that creates some directories, like this:

2018

    January

        1

        2

        ...

        31

    February

        1

        2

        ...

        3

2019

    January

        1

        2

        ...

        31

    February

        1

        2

        ...

        3

...

20XX
 

Unfortunately, I have to do this on Windows in a batch script. If I were using bash, I'd do this:
 

#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "Usage: $0 [StartYear] [EndYear]"
    echo "Ex: $0 2018 2050"
    exit 1
fi

NUMERIC_REGEX="[0-9]{4}"
MONTHS="January February March April May June July August September October November December"

startYear=$1
endYear=$2

if [[ ! "$startYear" =~ $NUMERIC_REGEX ]]; then { echo "\"$startYear\" is not a valid 4 digit year"; exit 1; } fi
if [[ ! "$endYear" =~ $NUMERIC_REGEX ]]; then { echo "\"$endYear\" is not a valid 4 digit year"; exit 1; } fi
if [[ $endYear -lt $startYear ]]; then { echo "EndYear must be greater than or equal to StartYear ($endYear < $startYear)"; exit 1; } fi

for (( year=startYear; year<=endYear; year++ )); do
    for month in $MONTHS; do
        for (( day=0; day<=31; day++ )); do
            targetDirectory="$year/$month/$day"
            mkdir "$targetDirectory" || { echo "Could not create directory \"$targetDirectory\", exiting"; exit 1; }
        done
    done
done

So example usage would be "PROGRAM_NAME.bat 2010 2015" and it would generate the folder structure for 2010 to 2015. Note that the goal is NOT to only create ex 28 folders in February, but to simply make 31 folders for every month. If the regex can't be translated to a batch script, that's fine. I'm more interested in how to convert this to a batch script, and I was hoping a batch expert could get me started.

Thanks.

Link to comment
https://linustechtips.com/topic/887349-simple-batch-script-help/
Share on other sites

Link to post
Share on other sites

Since you're not that familiar with windows batch scripts, I would say instead of using nested loops, just create several different batch files.

 

First batch file one takes as arguments a beginning year and ending year and the directory to put them all in (or instead of arguments, store them in variables inside)

Write a loop, starting at the first year until you get to the last year.

For each year, create a directory with that year, then pass that directory, and the year to the second batch file.

 

In the 2nd batch file, for each month, pass the year directory name, the month, and the year to the 3rd batch file.

 

In the 3rd batch file, create a directory with that month name

Determine the number of days in that month (check for leap years)

Loop and create a directory for each day.

Link to post
Share on other sites

On 1/18/2018 at 2:34 PM, JacobFW said:

Since you're not that familiar with windows batch scripts, I would say instead of using nested loops, just create several different batch files.

 

First batch file one takes as arguments a beginning year and ending year and the directory to put them all in (or instead of arguments, store them in variables inside)

Write a loop, starting at the first year until you get to the last year.

For each year, create a directory with that year, then pass that directory, and the year to the second batch file.

 

In the 2nd batch file, for each month, pass the year directory name, the month, and the year to the 3rd batch file.

 

In the 3rd batch file, create a directory with that month name

Determine the number of days in that month (check for leap years)

Loop and create a directory for each day.

Thanks, but I want to keep this confined to one script.
 

On 1/19/2018 at 1:38 AM, geo3 said:

Can you use powershell? It's way more  powerful and easy to use than a batch script. You can use a more C like syntax and you have direct access to create and manipulate .net objects. 

Unfortunately, I have to run this on Windows XP and Windows 2000 systems, so PowerShell is a no-go.

 

Link to post
Share on other sites

Okay, I figured it out. The (rough) translation of the shell script to Batch looks like this:

@ECHO off

set argC=0
for %%x in (%*) do Set /A argC+=1
if %argC% NEQ 2 (
    echo Usage: %0 [StartYear] [EndYear]
    echo Ex: %0 2018 2050
    EXIT /B 1
)

set startYear=%1
set endYear=%2

echo %startYear% | findstr "^[-][1-9][0-9]*$ ^[1-9][0-9]*$ ^0$">nul
if %ERRORLEVEL% EQU 0 (
    echo "%startYear%" is not a valid 4 digit year
    EXIT /B 1
)

echo %endYear% | findstr "^[-][1-9][0-9]*$ ^[1-9][0-9]*$ ^0$">nul
if %ERRORLEVEL% EQU 0 (
    echo "%endYear%" is not a valid 4 digit year
    EXIT /B 1
)

if %startYear% GTR %endYear% (
    echo EndYear must be greater than or equal to StartYear "(%endYear% < %startYear%)"
    EXIT /B 1
)

set months=(January February March April May June July August September October November December)

for /l %%y in (%startYear%,1,%endYear%) do (
    for %%m in %months% do (
        for /l %%d in (1, 1, 31) do (
            mkdir %%y\%%m\%%d
            if %ERRORLEVEL% EQU 0 (
                echo Successfully created directory %%y\%%m\%%d
            ) else (
                echo Could not create directory %%y\%%m\%%d, exiting script
                pause
                EXIT /B 1
            )
        )
    )
)

 

Link to post
Share on other sites

On ‎1‎/‎18‎/‎2018 at 10:21 AM, Pinguinsan said:

Hey guys. I need to create a batch script that creates some directories, like this:

2018

    January

        1

        2

        ...

        31

    February

        1

        2

        ...

        3

2019

    January

        1

        2

        ...

        31

    February

        1

        2

        ...

        3

...

20XX
 

Unfortunately, I have to do this on Windows in a batch script. If I were using bash, I'd do this:
 


#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "Usage: $0 [StartYear] [EndYear]"
    echo "Ex: $0 2018 2050"
    exit 1
fi

NUMERIC_REGEX="[0-9]{4}"
MONTHS="January February March April May June July August September October November December"

startYear=$1
endYear=$2

if [[ ! "$startYear" =~ $NUMERIC_REGEX ]]; then { echo "\"$startYear\" is not a valid 4 digit year"; exit 1; } fi
if [[ ! "$endYear" =~ $NUMERIC_REGEX ]]; then { echo "\"$endYear\" is not a valid 4 digit year"; exit 1; } fi
if [[ $endYear -lt $startYear ]]; then { echo "EndYear must be greater than or equal to StartYear ($endYear < $startYear)"; exit 1; } fi

for (( year=startYear; year<=endYear; year++ )); do
    for month in $MONTHS; do
        for (( day=0; day<=31; day++ )); do
            targetDirectory="$year/$month/$day"
            mkdir "$targetDirectory" || { echo "Could not create directory \"$targetDirectory\", exiting"; exit 1; }
        done
    done
done

So example usage would be "PROGRAM_NAME.bat 2010 2015" and it would generate the folder structure for 2010 to 2015. Note that the goal is NOT to only create ex 28 folders in February, but to simply make 31 folders for every month. If the regex can't be translated to a batch script, that's fine. I'm more interested in how to convert this to a batch script, and I was hoping a batch expert could get me started.

Thanks.

try going into addremove programs

then add components

see if BASH is listed(win10 has support for bash functionality)

Link to post
Share on other sites

20 hours ago, bcguru9384 said:

try going into addremove programs

then add components

see if BASH is listed(win10 has support for bash functionality)

Unfortunately, I have to run this on Windows XP and Windows 2000 systems, so WSL Bash is a no-go.

Link to post
Share on other sites

6 minutes ago, Pinguinsan said:

Unfortunately, I have to run this on Windows XP and Windows 2000 systems, so WSL Bash is a no-go.

try looking up building calendar in c64 basic then

this may give some nice tricks

also look at schoolfreeware youtube as the have a number of vids(i watched big chunk of c64 vids and think array linking/mergeing be helpful here as your batching)

also anything under dos would help as again batch is used(grab dos calender or old dos spreadsheet that has calender) 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×