How do I create a batch file that will create a folder for each day of the month?

1

At the beginning of each month inside a directory, I need to create a folder for each day in that month (in the format of DDMMYYYY). I'd like to just run this script on the first day of each month and account for the number of days in said month. I have played around with the following script, but not sure how to customize it to fit my needs:

How do I write a batch script to generate folders for each month, day and year?

Any thought/suggestions are greatly appreciated.

Aaron

Posted 2015-03-20T13:45:46.043

Reputation: 11

Question was closed 2015-03-21T05:50:36.613

Unfortunately, we are not a code-writing service. Instead of simply asking for code to perform a particular task, please show us what you've tried so far (including any code you currently have) and where you're stuck so that we can help you with your specific problem. Questions that only ask for code are too broad and are likely to be put on hold or closed.

– bwDraco – 2015-03-20T22:56:18.327

Answers

3

EDIT: I've missed DDMMYYYY requirement, fixed now.

Powershell one-liners. Just open the PowerShell console in your directory and run:

Compact:

1..([datetime]::DaysInMonth([datetime]::Now.Year,[datetime]::Now.Month)) | % {md ".\$(([datetime]::ParseExact("$_/$([datetime]::Now.Month)/$([datetime]::Now.Year)", 'd/M/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)).ToString('ddMMyyyy'))"}

No aliases:

1..([datetime]::DaysInMonth([datetime]::Now.Year,[datetime]::Now.Month)) | ForEach-Object {New-Item -ItemType Directory -Path ".\$(([datetime]::ParseExact("$_/$([datetime]::Now.Month)/$([datetime]::Now.Year)", 'd/M/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)).ToString('ddMMyyyy'))"}

Human-readable:

1..([datetime]::DaysInMonth([datetime]::Now.Year,[datetime]::Now.Month)) |
    ForEach-Object {
        New-Item -ItemType Directory -Path ".\$(([datetime]::ParseExact("$_/$([datetime]::Now.Month)/$([datetime]::Now.Year)", 'd/M/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)).ToString('ddMMyyyy'))"
    }

This is more script-like, you'd have to save it as CreateFolders.ps1 and run from PowerShell console like this: .\CreateFolders.ps1. Commented, so you can actually understand what's happening under the hood:

# Get current Year and Month
$CurrYear = [datetime]::Now.Year
$CurrMonth = [datetime]::Now.Month

# Create new array with numbers from 1 to DaysInMonth
1..([datetime]::DaysInMonth($CurrYear, $CurrMonth)) |
    # For each element in array
    ForEach-Object {
        # Generate name for new directory in ddMMyyyy format
        # To do so, we create new DateTime Object with day from pipeline and current year and month
        # Then we convert it to the abovementioned format
        $DirName = ([datetime]::ParseExact("$_/$CurrMonth/$CurrYear", 'd/M/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)).ToString('ddMMyyyy')

        # Correctly join path with current directory and new folder name
        $Path = Join-Path -Path (Get-Location).Path -ChildPath $DirName

        # Create new directory
        New-Item -ItemType Directory -Path $Path
    }

Related question: Get the number of Days in the current month using powershell

beatcracker

Posted 2015-03-20T13:45:46.043

Reputation: 2 334

-1 actually, I've missed DDMMYYYY requirement, going to fix it now. – beatcracker – 2015-03-20T14:06:40.730