test file script added

This commit is contained in:
urfari 2025-02-04 15:36:31 +01:00
parent 7182a0b887
commit 58a370dd43
1 changed files with 69 additions and 0 deletions

69
util/Create-TestFiles.ps1 Normal file
View File

@ -0,0 +1,69 @@
#!/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)) }