Automating Recursive Folder and File Creation in Windows with PowerShell

Introduction

In the realm of software development, the art of crafting intricate folder structures for testing or showcasing scenarios is both crucial and, at times, arduous. Yet, the manual creation of these structures often consumes valuable time and may introduce inadvertent errors. This article unveils a savvy solution: harnessing the power of PowerShell scripting to automate the generation of elaborate folder hierarchies effortlessly.

Let's dive deeper into the story of how developers can break free from the shackles of manual folder creation and embrace the efficiency and precision offered by PowerShell automation.

Script

The following script creates 2 levels of folders with 3 folders in each level. And each folder will contain 4 files. Adjust these numbers in the script according to the requirements.

# Function to create folders and files recursively
function CreateFoldersAndFiles {
    param (
        [string]$basePath,
        [int]$level
    )

    # Create 3 folders at the current level
    1..3 | ForEach-Object {
        $folderName = "Level$level-Folder$_"
        $folderPath = Join-Path -Path $basePath -ChildPath $folderName
        New-Item -ItemType Directory -Path $folderPath | Out-Null

        # Create 4 sample text files in each folder with level info in the name
        1..4 | ForEach-Object {
            $fileName = "SampleFile-Level$level-Folder$_-File$_" + ".txt"
            $filePath = Join-Path -Path $folderPath -ChildPath $fileName
			"This is a sample text file at Level${level}Folder${_}File$_" | Set-Content -Path $filePath
        }

        # Create child folders if not at the last level
        if ($level -lt 2) {
            CreateFoldersAndFiles -basePath $folderPath -level ($level + 1)
        }
    }
}

# Get the current date and time
$currentDateTime = Get-Date

# Format the date and time components
$year = $currentDateTime.Year.ToString()
$month = $currentDateTime.Month.ToString("00")
$day = $currentDateTime.Day.ToString("00")
$hour = $currentDateTime.Hour.ToString("00")
$minute = $currentDateTime.Minute.ToString("00")
$second = $currentDateTime.Second.ToString("00")

# Construct the folder name
$folderName = "root-$year$month$day-$hour$minute$second"

# Define the base path where the folders will be created
$basePath = Join-Path -Path $PSScriptRoot -ChildPath $folderName

# Create the parent folder if it doesn't exist
if (-not (Test-Path $basePath -PathType Container)) {
    New-Item -Path $basePath -ItemType Directory
}

# Create the folder structure
CreateFoldersAndFiles -basePath $basePath -level 1

Write-Host "Folder structure created successfully."

Running the Script:

In this example I stored the code under the folder C:\Users\vmaths\Documents\FoldersAndFiles\ and PowerShell Script is named as create-folders-files.ps1. After running the script, it will create a root level folder in the same folder where Script is running with the name root-yyyymmdd-hhmmss. For example: root-20170603-171753.

List the files in the folder where the script is stored

PS C:\Users\vmaths\Documents\FoldersAndFiles> Get-ChildItem -Path . -Recurse | Select-Object FullName

Result
FullName
--------
C:\Users\vmaths\Documents\FoldersAndFiles\create-folders-files.ps1

Run the script

PS C:\Users\vmaths\Documents\FoldersAndFiles> .\create-folders-files.ps1


Result
Directory: C:\Users\vmaths\Documents\FoldersAndFiles


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          3/6/2017   5:17 PM                root-20170603-171753
Folder structure created successfully.

List the created folders and files

PS C:\Users\vmaths\Documents\FoldersAndFiles> Get-ChildItem -Path . -Recurse | Select-Object FullName


Result:
FullName
--------
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753
C:\Users\vmaths\Documents\FoldersAndFiles\create-folders-files.ps1
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder1
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder2
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\SampleFile-Level1-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\SampleFile-Level1-Folder2-File2.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder1\SampleFile-Level2-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder1\SampleFile-Level2-Folder2-File2.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder2\SampleFile-Level2-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder1\Level2-Folder2\SampleFile-Level2-Folder2-File2.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder1
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder2
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\SampleFile-Level1-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\SampleFile-Level1-Folder2-File2.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder1\SampleFile-Level2-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder1\SampleFile-Level2-Folder2-File2.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder2\SampleFile-Level2-Folder1-File1.txt
C:\Users\vmaths\Documents\FoldersAndFiles\root-20170603-171753\Level1-Folder2\Level2-Folder2\SampleFile-Level2-Folder2-File2.txt

Conclusion:

By harnessing the power of PowerShell scripting, developers can automate the creation of sample folder structures, streamlining their workflow and enhancing productivity.

Post a comment