打造完整的文件處理流水線
定義:將多個獨立的步驟串連成一個自動化流程,讓工作能夠順暢進行
Email 附件自動處理
新檔案自動轉換
定時產生並寄送
與 Git 整合
與雲端硬碟整合
智慧分類整理
每個步驟只做一件事,容易維護和除錯
建立通用的元件,在不同流程中重複使用
任何步驟失敗都不會影響其他步驟
能夠追蹤每個步驟的執行狀態
目標:監控特定資料夾,新的 .md 檔案自動轉換成 .docx
1開啟 Antigravity
2請 AI 幫你建立監控腳本
請幫我建立一個 PowerShell 腳本,
可以監控指定資料夾,
當有新的 .md 檔案出現時,
自動使用 Pandoc 轉換成 .docx
# watch-folder.ps1
param(
[string]$WatchPath = "D:\待處理",
[string]$OutputPath = "D:\已完成"
)
# 確保輸出資料夾存在
if (!(Test-Path $OutputPath)) {
New-Item -ItemType Directory -Path $OutputPath | Out-Null
}
Write-Host "開始監控: $WatchPath" -ForegroundColor Cyan
Write-Host "輸出到: $OutputPath" -ForegroundColor Cyan
Write-Host "按 Ctrl+C 停止`n"
# 建立檔案監控器
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $WatchPath
$watcher.Filter = "*.md"
$watcher.EnableRaisingEvents = $true
# 當有新檔案時觸發
Register-ObjectEvent $watcher "Created" -Action {
$file = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$output = "$using:OutputPath\$([System.IO.Path]::ChangeExtension($name, '.docx'))"
Start-Sleep -Seconds 1 # 等待檔案寫入完成
pandoc $file -o $output
Write-Host "已轉換: $name" -ForegroundColor Green
}
# 保持腳本運行
while ($true) { Start-Sleep -Seconds 1 }
讓腳本在指定時間自動執行
# 使用 PowerShell 建立排程任務
$action = New-ScheduledTaskAction `
-Execute "PowerShell.exe" `
-Argument "-File D:\Scripts\daily-report.ps1"
$trigger = New-ScheduledTaskTrigger `
-Daily -At "09:00"
Register-ScheduledTask `
-TaskName "每日報告生成" `
-Action $action `
-Trigger $trigger `
-Description "每天早上 9 點自動生成報告"
# daily-report.ps1
$date = Get-Date -Format "yyyy-MM-dd"
$reportDir = "D:\報告\$date"
# 建立目錄
New-Item -ItemType Directory -Path $reportDir -Force | Out-Null
# 步驟 1: 收集資料
$data = @{
date = $date
sales = Get-Content "D:\數據\sales.json" | ConvertFrom-Json
tasks = Get-Content "D:\數據\tasks.csv" | ConvertFrom-Csv
}
# 步驟 2: 生成報告內容
$template = Get-Content "D:\模板\daily-template.md" -Raw
$report = $template -replace '\{\{date\}\}', $date
# 步驟 3: 儲存並轉換
$mdFile = "$reportDir\daily-report.md"
$docxFile = "$reportDir\daily-report.docx"
$report | Out-File $mdFile -Encoding UTF8
pandoc $mdFile -o $docxFile
Write-Host "日報已生成: $docxFile" -ForegroundColor Green
將輸出檔案自動同步到雲端
# 設定輸出到 OneDrive 同步資料夾
$outputPath = "$env:USERPROFILE\OneDrive\報告"
# 轉換後檔案會自動同步到雲端
pandoc report.md -o "$outputPath\report.docx"
Write-Host "報告已儲存並同步到雲端"
技巧:只要將輸出目錄設在 OneDrive 或 Google Drive 的同步資料夾,檔案就會自動上傳!
# git-publish.ps1
# 文件修改後自動提交和發布
param([string]$Message = "更新文件")
$docsPath = "D:\專案\docs"
# 轉換所有文件
Get-ChildItem "$docsPath\*.md" | ForEach-Object {
$output = [System.IO.Path]::ChangeExtension($_.FullName, ".html")
pandoc $_.FullName -o $output --standalone
}
# Git 操作
Set-Location $docsPath
git add .
git commit -m $Message
git push
Write-Host "文件已發布!" -ForegroundColor Green
一次轉換,多種格式輸出
# multi-format.ps1
param([string]$InputFile)
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($InputFile)
$outputDir = "output"
# 確保輸出目錄存在
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
# 定義要輸出的格式
$formats = @{
"docx" = ""
"html" = "--standalone --self-contained"
"pdf" = "--pdf-engine=xelatex"
}
foreach ($format in $formats.Keys) {
$output = "$outputDir\$baseName.$format"
$args = $formats[$format]
Write-Host "轉換為 $format..." -NoNewline
Invoke-Expression "pandoc $InputFile -o $output $args"
Write-Host " 完成" -ForegroundColor Green
}
Write-Host "`n所有格式已輸出到 $outputDir\"
根據檔案類型或內容決定處理方式
# smart-process.ps1
param([string]$InputFile)
# 讀取檔案判斷類型
$content = Get-Content $InputFile -Raw -Encoding UTF8
# 根據內容決定處理方式
if ($content -match "^---") {
Write-Host "偵測到 YAML 標頭,使用模板轉換"
pandoc $InputFile -o output.docx --reference-doc=template.docx
}
elseif ($content -match "```") {
Write-Host "偵測到程式碼,使用語法高亮"
pandoc $InputFile -o output.html --highlight-style=monokai
}
else {
Write-Host "一般文件,標準轉換"
pandoc $InputFile -o output.docx
}
# pipeline-process.ps1
# 處理流程:收集 → 過濾 → 轉換 → 輸出
Get-ChildItem "D:\收件匣\*.md" | # 收集檔案
Where-Object { $_.Length -gt 100 } | # 過濾太小的
ForEach-Object { # 轉換每個檔案
$output = "D:\輸出\$($_.BaseName).docx"
pandoc $_.FullName -o $output
# 回傳處理結果
[PSCustomObject]@{
Source = $_.Name
Output = $output
Size = (Get-Item $output).Length
}
} |
Export-Csv "處理報告.csv" -NoTypeInformation # 輸出報告
Write-Host "處理完成,請查看 處理報告.csv"
# 完成後顯示桌面通知
$notify = New-Object -ComObject WScript.Shell
$notify.Popup("報告已生成完成!", 5, "轉換完成", 64)
# 發送通知信件
$params = @{
To = "manager@company.com"
From = "system@company.com"
Subject = "日報已生成 - $date"
Body = "請查收附件中的日報"
Attachments = $docxFile
SmtpServer = "smtp.company.com"
}
Send-MailMessage @params
# complete-workflow.ps1
# 完整的文件處理工作流程
param(
[string]$InputFolder = "D:\待處理",
[string]$OutputFolder = "D:\已完成",
[switch]$Notify
)
Write-Host "=== 文件處理工作流程 ===" -ForegroundColor Cyan
$startTime = Get-Date
# 1. 收集待處理檔案
$files = Get-ChildItem "$InputFolder\*.md"
Write-Host "找到 $($files.Count) 個檔案待處理"
# 2. 處理每個檔案
$results = @()
foreach ($file in $files) {
try {
$output = "$OutputFolder\$($file.BaseName).docx"
pandoc $file.FullName -o $output
$results += [PSCustomObject]@{
File = $file.Name
Status = "成功"
}
}
catch {
$results += [PSCustomObject]@{
File = $file.Name
Status = "失敗: $_"
}
}
}
# 3. 輸出報告
$duration = (Get-Date) - $startTime
Write-Host "`n處理完成,耗時 $($duration.TotalSeconds.ToString('F1')) 秒"
# 4. 發送通知
if ($Notify) {
$shell = New-Object -ComObject WScript.Shell
$shell.Popup("處理完成!", 3, "工作流程", 64)
}
1選擇一個你常做的文件處理任務
2列出處理步驟(輸入→處理→輸出)
3用 Antigravity AI 幫你寫腳本
4測試並優化流程
5(進階)設定定時自動執行
我想要建立一個自動化流程:
1. 每天早上檢查特定資料夾的新檔案
2. 將 Markdown 檔案轉換成 Word
3. 移動到「已處理」資料夾
4. 產生處理報告
請幫我設計這個流程,並寫出完整的腳本
AI 會幫你:設計流程架構、撰寫腳本、加入錯誤處理
注意:自動化流程可能會持續運行,要確保不會產生意外的大量檔案或佔用過多資源
檔案變更、時間到達時觸發
累積一定數量後統一處理
手動執行或 API 呼叫觸發
與開發流程結合,程式碼變更時自動更新文件
哪個 PowerShell 類別可以用來監控資料夾變更?
恭喜!你已完成 Part 08 進階技巧與優化的學習!