Added a check if file is already existing

This commit is contained in:
Markus Fleschutz
2025-06-05 14:29:28 +02:00
parent f177f22cf8
commit 330b13c72b
4 changed files with 11 additions and 5 deletions

View File

@ -1,13 +1,13 @@
<# <#
.SYNOPSIS .SYNOPSIS
Creates a new Markdown file Creates a Markdown file
.DESCRIPTION .DESCRIPTION
This PowerShell script creates a new Markdown file from template file at: ../data/templates/Markdown.md. This PowerShell script creates a new Markdown file from template: ../data/templates/New.md.
.PARAMETER path .PARAMETER path
Specifies the path and new filename (README.md by default) Specifies the path and new filename (README.md by default)
.EXAMPLE .EXAMPLE
PS> ./new-markdown-file.ps1 PS> ./new-markdown-file.ps1
✅ New file 'README.md' created from template 'Markdown.md'. ✅ New 'README.md' created (from data/templates/New.md).
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -17,11 +17,13 @@
param([string]$path = "README.md") param([string]$path = "README.md")
try { try {
$pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/Markdown.md" if (Test-Path "$path" -pathType leaf)) { throw "File '$path' is already existing" }
$pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/New.md"
Copy-Item $pathToTemplate "$path" Copy-Item $pathToTemplate "$path"
if ($lastExitCode -ne 0) { throw "Can't copy template to: $path" } if ($lastExitCode -ne 0) { throw "Can't copy template to: $path" }
"✅ New file '$path' created from template 'Markdown.md'." "✅ New '$path' created (from data/templates/New.md)."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error: $($Error[0])" "⚠️ Error: $($Error[0])"

View File

@ -17,6 +17,8 @@
param([string]$path = "bot.ps1") param([string]$path = "bot.ps1")
try { try {
if (Test-Path "$path" -pathType leaf)) { throw "File '$path' is already existing" }
$pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/New.ps1" $pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/New.ps1"
Copy-Item $pathToTemplate "$path" Copy-Item $pathToTemplate "$path"
if ($lastExitCode -ne 0) { throw "Can't copy to: $path" } if ($lastExitCode -ne 0) { throw "Can't copy to: $path" }

View File

@ -17,6 +17,8 @@
param([string]$path = "README.txt") param([string]$path = "README.txt")
try { try {
if (Test-Path "$path" -pathType leaf)) { throw "File '$path' is already existing" }
$pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/New.txt" $pathToTemplate = Resolve-Path "$PSScriptRoot/../data/templates/New.txt"
Copy-Item $pathToTemplate "$path" Copy-Item $pathToTemplate "$path"
if ($lastExitCode -ne 0) { throw "Can't copy template to: $path" } if ($lastExitCode -ne 0) { throw "Can't copy template to: $path" }