Create monthly folders and daily subfolders for the whole year

I created a script that generates a specific path in the folder (first parameter) for each month ( yyyy_mm format) and in each of these folders subfolders for each day ( yyyy_mm_dd format).

The code works, but is there an easier solution?

 param( [string]$inppath = '', [string]$inpyear = '0' ) function dayfolder { 1..[DateTime]::DaysInMonth($inpyear,$month) | ForEach-Object { $day = $_ New-Item -ItemType directory -Force -Path ($inppath + '\' + $inpyear + '_' + ("{0:D2}" -f $month) + '\' + $inpyear + '_' + ("{0:D2}" -f $month) + '_' + ("{0:D2}" -f $day) ) } } if ($inppath -eq '') { echo 'No path in input! First parameter!' } else { if ($inpyear -eq '0') { echo 'No year in input! Second parameter! Format: YYYY' } else { 1..12 | ForEach-Object { $month = $_ New-Item -ItemType directory -Force -Path ($inppath + '\' + $inpyear + '_' + ("{0:D2}" -f $month)) dayfolder } } } 
+6
source share
3 answers

I think you make things overly complex by providing default values ​​for your arguments and then providing error messages when the default values ​​are used. If you want the parameters to be required, you must declare them as such. In fact, you can go further and declare them as a valid path and number in a certain range.

Otherwise, my main observation will be that New-Item creates any parent items that it needs, so you don't need to create monthly folders separately.

There are various ways to create a path string, but I think in this case it is easiest to format the month and day, and then just use a single string (note that as _ is a valid character in a variable name, in some cases you should use the t21 extension form ):

 param( # Path to an existing folder. [Parameter(Mandatory=$True)] [ValidateScript({Test-Path $_ -PathType 'Container'})] [string]$inppath, # Year must be fairly recent or in the future. Warranty expires 2100 [Parameter(Mandatory=$True)] [ValidateRange(1999,2100)] [int]$inpyear ) 1..12 | ForEach-Object { $month = "{0:D2}" -f $_ 1..[DateTime]::DaysInMonth($inpyear,$month) | ForEach-Object { $day = "{0:D2}" -f $_ New-Item -ItemType directory -Force -Path "$inppath\${inpyear}_$month\${inpyear}_${month}_${day}" } } 

Another thing is that if you want to use this often, it would be better to turn it into a function or cmdlet, and then you can save it in a module with other cmdlets. Also, if you do this, the comment before each parameter will become part of the help screen, and you can include a description and examples for the help screen in the comment at the top of the function declaration.

PS For bloody beginners, I recommend http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start

+5
source

Here is my approach. This is just the lack of parameter checking (I would do it like @Duncan):

 $inpyear = 2012 $inppath = 'D:\temp\ps' $startday = [datetime]"$inpyear-01-01" 0..(New-TimeSpan -Start $startday -End "$inpyear-12-31").TotalDays | select @{name='subpath'; expression={$startday.AddDays($_).ToString('yyyy_MM\\yyyy_MM_dd')}} | foreach { New-Item -ItemType Directory -Path (Join-Path $inppath -ChildPath $_.subpath) -Force } 
+3
source

Below is a Script:

 $path ="[Path]\" $months = "April 2018","May 2018","June 2018","July 2018","August 2018","September 2018","October 2018","November 2018","December 2018","January 2019","February 2019","March 2019" foreach($month in $months) { New-Item -ItemType Directory -Path "$path$month" } 
-1
source

Source: https://habr.com/ru/post/979680/


All Articles