|
| 1 | +# Makefile |
| 2 | + 82 │ function mkfile { |
| 3 | + 83 │ <# |
| 4 | + 84 │ .SYNOPSIS |
| 5 | + 85 │ Creates one or more files in the specified paths. |
| 6 | + 86 │ |
| 7 | + 87 │ .DESCRIPTION |
| 8 | + 88 │ The mkfile function creates one or more files in the specified paths. If a directory does not exist, it will b |
| 9 | + │ e created automatically. The function supports options for displaying version information and additional detai |
| 10 | + │ ls about the function itself. |
| 11 | + 89 │ |
| 12 | + 90 │ .PARAMETER FilePaths |
| 13 | + 91 │ Specifies the path(s) of the file(s) to be created. Wildcard characters are permitted. This parameter is manda |
| 14 | + │ tory if the -Version or -Info parameters are not specified. |
| 15 | + 92 │ |
| 16 | + 93 │ .PARAMETER Version |
| 17 | + 94 │ Displays the version information for the mkfile function. |
| 18 | + 95 │ |
| 19 | + 96 │ .PARAMETER Info |
| 20 | + 97 │ Displays additional information about the mkfile function, including its description, repository URL, and auth |
| 21 | + │ or. |
| 22 | + 98 │ |
| 23 | + 99 │ .INPUTS |
| 24 | + 100 │ System.String[] |
| 25 | + 101 │ The mkfile function accepts one or more file paths as input. |
| 26 | + 102 │ |
| 27 | + 103 │ .OUTPUTS |
| 28 | + 104 │ None |
| 29 | + 105 │ The mkfile function does not generate any output objects. |
| 30 | + 106 │ |
| 31 | + 107 │ .EXAMPLE |
| 32 | + 108 │ mkfile test.txt |
| 33 | + 109 │ Creates a file named 'test.txt' in the current directory. |
| 34 | + 110 │ |
| 35 | + 111 │ .EXAMPLE |
| 36 | + 112 │ mkfile C:\Temp\file1.txt C:\Temp\file2.txt |
| 37 | + 113 │ Creates two files, 'file1.txt' and 'file2.txt', in the 'C:\Temp' directory. |
| 38 | + 114 │ |
| 39 | + 115 │ .EXAMPLE |
| 40 | + 116 │ mkfile -Version |
| 41 | + 117 │ Displays the version information for the mkfile function. |
| 42 | + 118 │ |
| 43 | + 119 │ .EXAMPLE |
| 44 | + 120 │ mkfile -Info |
| 45 | + 121 │ Displays additional information about the mkfile function. |
| 46 | + 122 │ |
| 47 | + 123 │ .NOTES |
| 48 | + 124 │ Author: thecodermehedi |
| 49 | + 125 │ Repository: https://github.com/thecodermehedi/mkfile-powershell |
| 50 | + 126 │ Version: v1.0.0 |
| 51 | + 127 │ |
| 52 | + 128 │ .LINK |
| 53 | + 129 │ https://github.com/thecodermehedi/mkfile-powershell |
| 54 | + 130 │ #> |
| 55 | + 131 │ [CmdletBinding()] |
| 56 | + 132 │ param( |
| 57 | + 133 │ [Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] |
| 58 | + 134 │ [Alias('FullName')] |
| 59 | + 135 │ [string[]]$FilePaths, |
| 60 | + 136 │ |
| 61 | + 137 │ [Parameter(Mandatory=$false)] |
| 62 | + 138 │ [Alias('v')] |
| 63 | + 139 │ [switch]$Version, |
| 64 | + 140 │ |
| 65 | + 141 │ [Parameter(Mandatory=$false)] |
| 66 | + 142 │ [Alias('i')] |
| 67 | + 143 │ [switch]$Info |
| 68 | + 144 │ ) |
| 69 | + 145 │ |
| 70 | + 146 │ if ($Version) { |
| 71 | + 147 │ Write-Host "v1.0.0" |
| 72 | + 148 │ return |
| 73 | + 149 │ } |
| 74 | + 150 │ |
| 75 | + 151 │ if ($Info) { |
| 76 | + 152 │ Write-Host "mkfile: A PowerShell function to create files." |
| 77 | + 153 │ Write-Host "Repository: https://github.com/thecodermehedi/mkfile-powershell" |
| 78 | + 154 │ Write-Host "Author: thecodermehedi" |
| 79 | + 155 │ Write-Host "Version: v1.0.0" |
| 80 | + 156 │ return |
| 81 | + 157 │ } |
| 82 | + 158 │ |
| 83 | + 159 │ if (-not $FilePaths -or $FilePaths.Count -eq 0) { |
| 84 | + 160 │ Write-Host "Usage: mkfile [-?] [-v] [-i] <file_path> [<file_path>...]" |
| 85 | + 161 │ return |
| 86 | + 162 │ } |
| 87 | + 163 │ |
| 88 | + 164 │ process { |
| 89 | + 165 │ foreach ($FilePath in $FilePaths) { |
| 90 | + 166 │ try { |
| 91 | + 167 │ $FileDir = Split-Path -Path $FilePath -Parent |
| 92 | + 168 │ $FileName = Split-Path -Path $FilePath -Leaf |
| 93 | + 169 │ |
| 94 | + 170 │ if (-not (Test-Path -Path $FileDir)) { |
| 95 | + 171 │ $null = New-Item -Path $FileDir -ItemType Directory -Force |
| 96 | + 172 │ } |
| 97 | + 173 │ |
| 98 | + 174 │ $null = New-Item -Path $FilePath -ItemType File -Force |
| 99 | + 175 │ |
| 100 | + 176 │ Write-Host "File '$FileName' created successfully." |
| 101 | + 177 │ } |
| 102 | + 178 │ catch { |
| 103 | + 179 │ Write-Host "Error: $_" -ForegroundColor Red |
| 104 | + 180 │ } |
| 105 | + 181 │ } |
| 106 | + 182 │ } |
| 107 | + 183 │ } |
0 commit comments