Search code examples
powershelldatebatch-filedirectorysubdirectory

create a folder structure with name and date +7 days


i want to create a folder name with a start of W1 09-09-2021 (this is the name of the folder) i want the second one to be W2 + 7 days is there a way to create that in batch ? for let say 6 months ? Regards


Solution

  • You can use something like this. The could be more compressed but since you're a beginner I believe will learn more if I leave it like this.

    Remember to update the $folderPath variable using the destination Full Path where the new folders should be created.

    $startDate = [datetime]'09-09-2021'
    $endDate = $startDate.AddMonths(6)
    $startWeek = 1
    $folderPath = "X:\fullPath\to\newFolders"
    $datesToExclude = @(
        '12-30-2021' # This will Exclude 30 of December 2021
    ) | ForEach-Object {$_ -as [datetime]}
    
    do
    {   
        if($startDate -notin $datesToExclude)
        {
            $folderName = "W{0} {1}" -f $startWeek, $startDate.ToString('MM-dd-yyyy')
            $newFolderPath = Join-Path $folderPath -ChildPath $folderName
            New-Item $newFolderPath -ItemType Directory
        }
    
        $startDate = $startDate.AddDays(7)
        $startWeek++
    
    }until($startDate -ge $endDate)
    

    New folders should look like this from W1 until W26:

    W1 09-09-2021
    W2 09-16-2021
    W3 09-23-2021
    W4 09-30-2021
    ...
    ...
    ...
    W24 02-17-2022
    W25 02-24-2022
    W26 03-03-2022