-
Notifications
You must be signed in to change notification settings - Fork 1
/
getnextnumber.ps1
39 lines (32 loc) · 1005 Bytes
/
getnextnumber.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Check if folder exists
if (-not (Test-Path "AdLerDokumentation/Writerside/topics" -PathType Container)) {
Write-Output "Folder 'AdLerDokumentation/Writerside/topics' does not exist."
exit 1
}
# Check if shorthand is provided as argument
if ($args.Count -eq 0) {
Write-Output "Usage: $PSCommandPath <shorthand> [--short]"
exit 1
}
$shorthand = $args[0]
$folder = "AdLerDokumentation/Writerside/topics"
# Find all files with the given shorthand
$files = Get-ChildItem -Path $folder -Filter "$shorthand*.md" | Sort-Object Name
# Extract the numbers from the files
$numbers = $files.Name -replace "^$shorthand(\d+)\.md$", '$1'
# Find the first gap in the sequence
$gap = 1
foreach ($number in $numbers) {
$number = $number -replace '^0+', ''
if ($number -ne $gap) {
break
}
$gap++
}
# Format the file name with the gap
if($args[1] -eq "--short") {
$next_file = "{0}{1}.md" -f $shorthand, $gap
} else {
$next_file = "{0}{1:D4}.md" -f $shorthand, $gap
}
Write-Output $next_file