70 lines
1.9 KiB
PowerShell
70 lines
1.9 KiB
PowerShell
#!/usr/bin/pswh
|
|
|
|
#
|
|
# author: arian
|
|
# date: 2025-02-04
|
|
#
|
|
|
|
#
|
|
# Creates a random sub-tree of files and directories, up to $MaxDepth levels deep. All files are
|
|
# named with random GUID names and have a random "last modified" timestamp. Use this only for
|
|
# test files within the directory you're currently in.
|
|
#
|
|
# Usage: /path/to/script
|
|
# Usage: /path/to/script -MaxDepth 10
|
|
# Usage: /path/to/script -m 10
|
|
# Usage: /path/to/script -MaxDepth 10 -Iterations 10
|
|
# Usage: /path/to/script -m 10 -i 10
|
|
# Usage: /path/to/script -MaxDepth 10 -Iterations 10 -ChangeLastModified
|
|
# Usage: /path/to/script -m 10 -i 10 -c
|
|
#
|
|
# Using a higher $Iterations takes significantly longer; $MaxDepth doesn't seem to affect the
|
|
# performance as much
|
|
#
|
|
|
|
param(
|
|
[Alias("m")][int]$MaxDepth = 5,
|
|
[Alias("i")][int]$Iterations = 3,
|
|
[Alias("c")][bool]$ChangeLastModified
|
|
)
|
|
|
|
function Generate-RandomStructure {
|
|
param(
|
|
[int]$Depth,
|
|
[int]$MaxDepth,
|
|
[int]$Iterations
|
|
)
|
|
|
|
if ($Depth -gt $MaxDepth) {
|
|
return
|
|
}
|
|
|
|
for ($i = 0; $i -lt $Iterations; $i++) {
|
|
$randomNumber = Get-Random -Minimum 0 -Maximum 101
|
|
|
|
if ($randomNumber % 2 -eq 0) {
|
|
$fileName = "$([guid]::NewGuid()).txt"
|
|
Write-Host "Creating file: $fileName"
|
|
New-Item -Path "." -Name $fileName -ItemType "File"
|
|
} else {
|
|
$directoryName = "$([guid]::NewGuid()).d"
|
|
Write-Host "Creating directory: $directoryName"
|
|
New-Item -Path "." -Name $directoryName -ItemType "Directory"
|
|
|
|
Set-Location -Path $directoryName
|
|
Generate-RandomStructure -Depth ($Depth + 1) -MaxDepth $MaxDepth -Iterations $Iterations
|
|
Set-Location -Path ".."
|
|
}
|
|
}
|
|
}
|
|
|
|
Generate-RandomStructure -Depth 1 -MaxDepth $MaxDepth -Iterations $Iterations
|
|
|
|
Write-Output "Generation finished:`n`n`n`n`n`n"
|
|
|
|
tree /F
|
|
|
|
Write-Output "Applying random last modified date"
|
|
Get-ChildItem -Recurse -File | ForEach-Object { $_.LastWriteTime = (Get-Date).AddDays( - (Get-Random -Minimum 0 -Maximum 120)) }
|
|
|