last ...
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
# Script: Merge-TextFiles.ps1
|
||||
# Description: ادغام تمام فایلهای متنی در یک پوشه به یک فایل با نام حاوی تاریخ
|
||||
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$SourceDirectory,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$OutputFileName = "MergedContent",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string[]]$Extensions = @("*.txt", "*.md", "*.csv", "*.json", "*.xml")
|
||||
)
|
||||
|
||||
# بررسی وجود پوشه منبع
|
||||
if (-not (Test-Path $SourceDirectory)) {
|
||||
Write-Error "Specified Source Path doesn't Exists: $SourceDirectory"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# تولید نام فایل خروجی با تاریخ و زمان
|
||||
$timestamp = Get-Date -Format "yyyyMMdd_HHmmss"
|
||||
$outputFile = Join-Path $SourceDirectory "$OutputFileName`_$timestamp.txt"
|
||||
|
||||
Write-Host "Starting processing..." -ForegroundColor Cyan
|
||||
Write-Host "Source directory: $SourceDirectory" -ForegroundColor Yellow
|
||||
Write-Host "Output file: $outputFile" -ForegroundColor Yellow
|
||||
|
||||
# جمعآوری تمام فایلهای متنی
|
||||
$allFiles = @()
|
||||
foreach ($ext in $Extensions) {
|
||||
$files = Get-ChildItem -Path $SourceDirectory -Filter $ext -File -Recurse
|
||||
$allFiles += $files
|
||||
}
|
||||
|
||||
if ($allFiles.Count -eq 0) {
|
||||
Write-Warning "هیچ فایل متنی یافت نشد!"
|
||||
exit 0
|
||||
}
|
||||
|
||||
Write-Host "تعداد فایلهای یافت شده: $($allFiles.Count)" -ForegroundColor Green
|
||||
|
||||
# ایجاد فایل خروجی و نوشتن محتوا
|
||||
try {
|
||||
# هدر فایل
|
||||
$header = @"
|
||||
==================================================
|
||||
فایل ادغام شده از پوشه: $SourceDirectory
|
||||
تاریخ ایجاد: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')
|
||||
تعداد فایلها: $($allFiles.Count)
|
||||
==================================================
|
||||
|
||||
"@
|
||||
|
||||
Set-Content -Path $outputFile -Value $header -Encoding UTF8
|
||||
|
||||
# پردازش هر فایل
|
||||
$counter = 1
|
||||
foreach ($file in $allFiles) {
|
||||
Write-Progress -Activity "در حال پردازش فایلها" -Status "فایل $counter از $($allFiles.Count): $($file.Name)" -PercentComplete (($counter / $allFiles.Count) * 100)
|
||||
|
||||
# افزودن جداکننده و نام فایل
|
||||
$separator = "`n`n--------------------------------------------------`n"
|
||||
$fileHeader = "فایل: $($file.FullName)`nمسیر نسبی: $($file.FullName.Replace($SourceDirectory, '').TrimStart('\'))`nحجم: $($file.Length) بایت`n--------------------------------------------------`n`n"
|
||||
|
||||
Add-Content -Path $outputFile -Value $separator -Encoding UTF8
|
||||
Add-Content -Path $outputFile -Value $fileHeader -Encoding UTF8
|
||||
|
||||
# خواندن و نوشتن محتوای فایل
|
||||
try {
|
||||
$content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
|
||||
Add-Content -Path $outputFile -Value $content -Encoding UTF8
|
||||
}
|
||||
catch {
|
||||
Write-Warning "خطا در خواندن فایل: $($file.Name) - $_"
|
||||
Add-Content -Path $outputFile -Value "[خطا در خواندن این فایل]" -Encoding UTF8
|
||||
}
|
||||
|
||||
$counter++
|
||||
}
|
||||
|
||||
Write-Progress -Activity "در حال پردازش فایلها" -Completed
|
||||
|
||||
# نمایش اطلاعات نهایی
|
||||
$outputFileInfo = Get-Item $outputFile
|
||||
Write-Host "`nعملیات با موفقیت انجام شد!" -ForegroundColor Green
|
||||
Write-Host "فایل خروجی: $($outputFileInfo.FullName)" -ForegroundColor Cyan
|
||||
Write-Host "حجم فایل: $([math]::Round($outputFileInfo.Length / 1KB, 2)) کیلوبایت" -ForegroundColor Cyan
|
||||
|
||||
}
|
||||
catch {
|
||||
Write-Error "خطا در ایجاد فایل خروجی: $_"
|
||||
exit 1
|
||||
}
|
||||
Reference in New Issue
Block a user