Forked from The-Running-Dev/Create Kavita Directory Structure
Created
February 6, 2025 08:41
Revisions
-
The-Running-Dev revised this gist
Jun 8, 2023 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,11 @@ <# Save to: Create-Kavita-Structure.ps1 and run it with: .\Create-Kavita-Structure.ps1 -sourceDir 'DriveLetter:\PathToBooks' -kavitaDir 'DriveLetter:\PathToKavitaWatchedDir" If sourceDir is not specified, assumes current directory (.) If kavitaDir is not specified, assumes @Kavita under the current directory (.\@Kavita) To test this without making any changes: Create-Kavita-Structure.ps1 -whatIf #> [CmdletBinding(SupportsShouldProcess = $true)] Param( [Parameter(Mandatory = $false)][ValidateScript( { Test-Path (Resolve-Path $_) })][string] $sourceDir = (Resolve-Path .), -
The-Running-Dev revised this gist
Jun 8, 2023 . No changes.There are no files selected for viewing
-
The-Running-Dev created this gist
Jun 8, 2023 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,49 @@ [CmdletBinding(SupportsShouldProcess = $true)] Param( [Parameter(Mandatory = $false)][ValidateScript( { Test-Path (Resolve-Path $_) })][string] $sourceDir = (Resolve-Path .), [Parameter(Mandatory = $false)][ValidateScript( { Test-Path (Resolve-Path $_) })][string] $kavitaDir = (Resolve-Path '.\@Kavita') ) # List of type level directories to exclude $excludelist = '\@(Recycle|Kavita|Audiobookshelf|Plex)' # Get top level directories $directories = Get-ChildItem $sourceDir -Directory | Where-Object Name -NotMatch $excludelist # Create the top level structure inside the Kavita directory $directories | ForEach-Object { $subDir = Join-Path $kavitaDir $_.Name if ($PSCmdlet.ShouldProcess($subDir, 'Create')) { if (-not (Test-Path $subDir -ErrorAction SilentlyContinue)) { Write-Host "Creating Directory $subDir" New-Item -ItemType Directory $subDir | Out-Null } }} # Create the sub directories and hard links to files $directories | ` ForEach-Object { $subDir = Join-Path $kavitaDir $_.Name $files = Get-ChildItem $($_.FullName) -Include *.pdf, *.epub, *.chm -File -Recurse $files | Select-Object -First 5 | ForEach-Object { $bookSubDirectory = Join-Path $subDir $_.BaseName $bookSymLink = Join-Path $bookSubDirectory $_.Name if ($PSCmdlet.ShouldProcess($bookSubDirectory, "Create Directory")) { # Create a sub directory for the file if (-not (Test-Path $bookSubDirectory -ErrorAction SilentlyContinue)) { Write-Host "Creating Directory $bookSubDirectory" New-Item -ItemType Directory $bookSubDirectory | Out-Null } } if ($PSCmdlet.ShouldProcess($bookSymLink, "Create Hard Link")) { if (-not (Test-Path $bookSymLink -ErrorAction SilentlyContinue)) { Write-Host "Creating Hard Link $bookSymLink" New-Item -ItemType HardLink -Path $bookSymLink -Value $_.FullName | Out-Null } } } }