-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkfile.ps1
66 lines (56 loc) · 1.95 KB
/
mkfile.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Makefile
function mkfile {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
[string[]]$FilePaths,
[Parameter(Mandatory=$false)]
[Alias('v')]
[switch]$Version,
[Parameter(Mandatory=$false)]
[Alias('i')]
[switch]$Info
)
if ($Version) {
Write-Host "v1.0.0"
return
}
if ($Info) {
Write-Host "mkfile: A PowerShell function to create files."
Write-Host "Repository: https://github.com/thecodermehedi/mkfile-powershell"
Write-Host "Author: thecodermehedi"
Write-Host "Version: v1.0.0"
return
}
if (-not $FilePaths -or $FilePaths.Count -eq 0) {
Write-Host "Usage: mkfile [-?] [-v] [-i] <file_path> [<file_path>...]"
return
}
$BaseDirectory = (Get-Location).Path
foreach ($FilePath in $FilePaths) {
try {
# Construct the full path without requiring the file to exist
$FullResolvedPath = Join-Path -Path $BaseDirectory -ChildPath $FilePath
# Ensure the resolved path is within the base directory
if (-not $FullResolvedPath.StartsWith($BaseDirectory, [StringComparison]::OrdinalIgnoreCase)) {
Write-Host "Error: Target path is outside the base directory." -ForegroundColor Red
continue
}
# Directory check and file creation logic
$FileDir = Split-Path -Path $FullResolvedPath -Parent
if (-not (Test-Path -Path $FileDir)) {
$null = New-Item -Path $FileDir -ItemType Directory -Force
Write-Host "Directory '$FileDir' created."
}
if (-not (Test-Path -Path $FullResolvedPath)) {
$null = New-Item -Path $FullResolvedPath -ItemType File -Force
Write-Host "File '$FullResolvedPath' created successfully."
} else {
Write-Host "File '$FullResolvedPath' already exists."
}
} catch {
Write-Host "Error creating '$FullResolvedPath': $_" -ForegroundColor Red
}
}
}