Skip to content

Commit

Permalink
feat(FolderContent.ps1): add new PowerShell function Get-AllTextConte…
Browse files Browse the repository at this point in the history
…nt for reading text file contents in a path
  • Loading branch information
Bluzzi committed Aug 29, 2024
1 parent e4e056e commit 75bcca6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Core/Functions/FolderContent.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Function to read the content of all text files in a directory and its subdirectories, or a single file
function Get-AllTextContent {
param (
[string]$path
)

# Resolve the full path
$fullPath = Resolve-Path -Path $path -ErrorAction SilentlyContinue

if (-Not $fullPath) {
Write-Error "The specified path does not exist."
return
}

# Initialize a variable to store all the content
$allTextContent = ""

if (Test-Path -Path $fullPath -PathType Container) {
# If the path is a directory, get all files recursively
$files = Get-ChildItem -Path $fullPath -Recurse -File

foreach ($file in $files) {
try {
# Read the content of the file
$fileContent = Get-Content -Path $file.FullName -ErrorAction Stop
# Append the file content to the variable
$allTextContent += $fileContent + "`r`n"
} catch {
Write-Host "Error reading file: $($file.FullName)"
}
}
} elseif (Test-Path -Path $fullPath -PathType Leaf) {
try {
# Read the content of the single file
$allTextContent = Get-Content -Path $fullPath -ErrorAction Stop
} catch {
Write-Host "Error reading file: $path"
}
} else {
Write-Error "The specified path is neither a file nor a directory."
return
}

return $allTextContent
}

0 comments on commit 75bcca6

Please sign in to comment.