Search code examples
powershellmkdir

Creating multiple folders with dates etc. 190101 to 191231


I'm trying to create multiple folders but for dates in a year. Also, I have been using PowerShell and trying to create a batch script. I had tried several solutions, but no one of them gives me what I need. So, I need to create folders as 190101 to 191231 empty once for all year. But whatever I do I don't get what I want.

Examples:

01..31 | foreach $_{ New-Item -ItemType Directory -Name $("1901" + $_)}

mkdir $(01..31 | %{"ch$_"})

md(01..31|%{"1901$_"})

But the problem here they don't give me 0 in "days" so, I have 19011 instead of 190101.

I can't find how to extract dates and push PowerShell to create what I need.


Solution

  • Use the format operator (-f). It was made for this exact purpose.

    1..31 | ForEach-Object {
        New-Item -Type Directory -Name ('1901{0:d2}' -f $_)
    }