Automate Recursive Directory Creation With PowerShell: Build Complex Folder Structures Easily!

by FuzzyPotato in Circuits > Software

41 Views, 2 Favorites, 0 Comments

Automate Recursive Directory Creation With PowerShell: Build Complex Folder Structures Easily!

PowerShell Directory .PNG

Welcome to this instructable on creating a recursive directory structure with PowerShell!

In this tutorial, we'll explore how to generate a complex directory structure with varying depths, widths, and file counts using PowerShell. This script will allow you to automate the creation of directories and files, perfect for testing or organising large sets of data.

By the end of this instructable, you'll have a script that creates a customized directory structure with random subdirectories and files at each level. Let's dive in and get started!

Supplies

To create a recursive directory structure with PowerShell, you'll need:

  1. Windows computer with PowerShell installed

Set Up the Script

We'll start by setting up the parameters and defining the functions needed to create the directory structure and files.

Create a new PowerShell script and copy the following code:


param (
[string]$BasePath = "C:\Users\john.doe\TestDirectory", # Base path where the root directory will be created
[int]$Depth = 3, # How deep the subdirectories go
[int]$MinWidth = 2, # Minimum number of subdirectories at each level
[int]$MaxWidth = 10, # Maximum number of subdirectories at each level
[int]$MinFiles = 3, # Minimum number of files in each directory
[int]$MaxFiles = 100 # Maximum number of files in each directory
)

# Construct the root directory path
$RootPath = Join-Path -Path $BasePath -ChildPath "TestDirectory"

function Create-DirectoryStructure {
param (
[string]$CurrentPath,
[int]$CurrentDepth,
[int]$LayerNumber
)

if ($CurrentDepth -le 0) {
return
}

$NumSubdirs = Get-Random -Minimum $MinWidth -Maximum $MaxWidth

for ($i = 0; $i -lt $NumSubdirs; $i++) {
$SubdirName = "Layer${LayerNumber}_Subdir_$i"
$NewPath = Join-Path $CurrentPath $SubdirName
New-Item -ItemType Directory -Path $NewPath -Force | Out-Null
Create-Files $NewPath
Create-DirectoryStructure -CurrentPath $NewPath -CurrentDepth ($CurrentDepth - 1) -LayerNumber ($LayerNumber + 1)
}
}

function Create-Files {
param (
[string]$DirPath
)

$NumFiles = Get-Random -Minimum $MinFiles -Maximum $MaxFiles

for ($j = 0; $j -lt $NumFiles; $j++) {
$FilePath = Join-Path $DirPath ("File_$j.txt")
New-Item -ItemType File -Path $FilePath -Force | Out-Null
}
}

# Create the root directory
New-Item -ItemType Directory -Path $RootPath -Force | Out-Null

# Start recursive creation of directories and files
Create-DirectoryStructure -CurrentPath $RootPath -CurrentDepth $Depth -LayerNumber 1

Run the Script

Now that we have our script set up, let's run it and see the directory structure in action.

  1. Open PowerShell on your Windows computer.
  2. Navigate to the directory where your script is saved.
  3. Run the script by typing .\YourScriptName.ps1 and pressing Enter.


Explore the Generated Structure

Directory Snip.PNG

After running the script, you'll notice that the specified base path now contains a new directory named "TestDirectory." Inside, you'll find several layers of subdirectories, each containing a random number of files.

  1. The script creates subdirectories up to the specified depth.
  2. Each subdirectory contains a random number of files.
  3. The structure varies based on the parameters you set at the beginning.


Customize and Experiment

You've successfully created a recursive directory structure using PowerShell. This script is a powerful tool for automating directory and file creation, making it easier to organize or test large datasets.


Feel free to adjust the parameters at the top of the script to create different directory structures. You can change the depth, width, and file counts to suit your needs. Experiment with different values to see how the structure changes!


Now that you have this script, you can easily adapt it for various projects, whether you're testing software, organizing files, or setting up a complex directory structure. Happy scripting!

Congratulations!

Celebrate-free-celebration-clip-art-pictures-2.jpg

If you found this helped or was valuable in anyway please drop a like! It helps more than you know!