Skip to content

Instantly share code, notes, and snippets.

@valadas
Created February 23, 2024 19:14
Show Gist options
  • Save valadas/96076c26763e941cf5c541620a443285 to your computer and use it in GitHub Desktop.
Save valadas/96076c26763e941cf5c541620a443285 to your computer and use it in GitHub Desktop.
Create random folder/files structure
param (
[string]$rootPath = (Get-Location).Path,
[int]$maxDepth = 3,
[int]$maxFoldersPerLevel = 4,
[int]$maxFilesPerFolder = 5
)
function New-RandomDirectoryStructure {
param (
[string]$rootPath,
[int]$maxDepth,
[int]$maxFoldersPerLevel,
[int]$maxFilesPerFolder,
[int]$currentDepth = 0
)
if ($currentDepth -ge $maxDepth) {
return
}
if (-not (Test-Path $rootPath)) {
New-Item -ItemType Directory -Path $rootPath | Out-Null
}
1..(Get-Random -Minimum 1 -Maximum $maxFoldersPerLevel) | ForEach-Object {
$newDirName = Get-RandomString -length 5
$newDirPath = Join-Path $rootPath $newDirName
New-Item -ItemType Directory -Path $newDirPath | Out-Null
1..(Get-Random -Minimum 1 -Maximum $maxFilesPerFolder) | ForEach-Object {
$newFileName = Get-RandomString -length 5 + ".txt"
$newFilePath = Join-Path $newDirPath $newFileName
"Sample content: $(Get-RandomString -length 20)" | Out-File -FilePath $newFilePath
}
New-RandomDirectoryStructure -rootPath $newDirPath -maxDepth $maxDepth -maxFoldersPerLevel $maxFoldersPerLevel -maxFilesPerFolder $maxFilesPerFolder -currentDepth ($currentDepth + 1)
}
}
function Get-RandomString {
param (
[int]$length = 10
)
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
$randomString = -join ((1..$length) | ForEach-Object { Get-Random -InputObject $chars.ToCharArray() })
return $randomString
}
New-RandomDirectoryStructure -rootPath $rootPath -maxDepth $maxDepth -maxFoldersPerLevel $maxFoldersPerLevel -maxFilesPerFolder $maxFilesPerFolder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment