TheLandYacht

Yearmonthday directory

Dec 22nd, 2021
2,006
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Get current Year and Month
  2. $CurrYear = [datetime]::Now.Year
  3. $CurrMonth = [datetime]::Now.Month
  4.  
  5. # Create new array with numbers from 1 to DaysInMonth
  6. 1..([datetime]::DaysInMonth($CurrYear, $CurrMonth)) |
  7.     # For each element in array
  8.     ForEach-Object {
  9.         # Generate name for new directory in ddMMyyyy format
  10.         # To do so, we create new DateTime Object with day from pipeline and current year and month
  11.         # Then we convert it to the abovementioned format
  12.         $DirName = ([datetime]::ParseExact("$_/$CurrMonth/$CurrYear", 'd/M/yyyy', [System.Globalization.CultureInfo]::InvariantCulture)).ToString('ddMMyyyy')
  13.  
  14.         # Correctly join path with current directory and new folder name
  15.         $Path = Join-Path -Path (Get-Location).Path -ChildPath $DirName
  16.  
  17.         # Create new directory
  18.         New-Item -ItemType Directory -Path $Path
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment