41 lines
1.5 KiB
PowerShell
41 lines
1.5 KiB
PowerShell
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$Source,
|
|
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$Destination
|
|
)
|
|
|
|
try {
|
|
# 1. 路径预处理:展开环境变量 (例如把 $env:APPDATA 变成 C:\Users\...\AppData\Roaming)
|
|
$ResolvedSource = $ExecutionContext.InvokeCommand.ExpandString($Source)
|
|
$ResolvedDest = $ExecutionContext.InvokeCommand.ExpandString($Destination)
|
|
|
|
# 2. 处理相对路径 (针对源文件)
|
|
# 如果源路径是相对路径 (./assets/...), 尝试将其转换为绝对路径
|
|
# 假设脚本是从项目根目录调用的
|
|
if (-not (Test-Path $ResolvedSource) -and (Test-Path "$PWD\$ResolvedSource")) {
|
|
$ResolvedSource = Join-Path $PWD $ResolvedSource
|
|
}
|
|
|
|
# 3. 再次检查源文件是否存在
|
|
if (-not (Test-Path $ResolvedSource -PathType Leaf)) {
|
|
Write-Warning "`n[FileCopy] [Skip] File not found: $ResolvedSource"
|
|
return # 退出脚本
|
|
}
|
|
|
|
# 4. 处理目标目录 (自动创建不存在的文件夹)
|
|
$DestDir = Split-Path -Path $ResolvedDest -Parent
|
|
if (-not (Test-Path $DestDir)) {
|
|
Write-Host "`n[FileCopy] Create dir: $DestDir" -ForegroundColor DarkGray
|
|
New-Item -Path $DestDir -ItemType Directory -Force | Out-Null
|
|
}
|
|
|
|
# 5. 执行复制 (Force = 覆盖)
|
|
Copy-Item -Path $ResolvedSource -Destination $ResolvedDest -Force -ErrorAction Stop
|
|
|
|
Write-Host "`n[FileCopy] Success: $ResolvedDest" -ForegroundColor Green
|
|
|
|
} catch {
|
|
Write-Error "`n[FileCopy] Fail: $_"
|
|
} |