ps1程序 移动下载文件夹的 文件

ps1移动下载文件夹的 文件

编写ps1脚本,模块化程序,编写注释,复制win11当前用户下载文件夹下的特定文件,到指定变量 内的 路径,特定文件名字特征 ① user-240-174.rar ,显示复制进度。

文件名匹配模式(user-<数字>-<数字>.rar 和 pastebin_backup_<YYYYMMDD>.zip)、进度条显示、错误处理等均未改变。
目标路径仍为 D:\Backup\Files,可根据需要修改 $destination 变量。

脚本仍从当前用户下载文件夹(%USERPROFILE%\Downloads)查找文件。

使用说明:
保存脚本为 Move-SpecificFiles.ps1。

修改 $destination 变量为目标路径。
在PowerShell中运行:.\Move-SpecificFiles.ps1。
移动操作会将文件从源路径移除并移到目标路径,覆盖同名文件。

模块化PowerShell脚本,用于移动特定文件并显示进度

获取当前用户下载文件夹路径

function Get-DownloadsFolder {
[CmdletBinding()]
param ()

try {
    $downloadsPath = [System.IO.Path]::Combine($env:USERPROFILE, "Downloads")
    if (-not (Test-Path $downloadsPath)) {
        throw "下载文件夹不存在: $downloadsPath"
    }
    return $downloadsPath
}
catch {
    Write-Error $_.Exception.Message
    exit 1
}

}

检查目标路径是否有效

function Test-DestinationPath {
[CmdletBinding()]
param (
[Parameter(Mandatory=true)] [string]Path
)

try {
    if (-not (Test-Path $Path)) {
        New-Item -ItemType Directory -Path $Path -Force | Out-Null
    }
    return $true
}
catch {
    Write-Error "无法创建或访问目标路径: $Path"
    return $false
}

}

获取匹配特定模式的文件

function Get-TargetFiles {
[CmdletBinding()]
param (
[Parameter(Mandatory=true)] [string]SourcePath
)

$rarPattern = "user-*-*.rar"
$zipPattern = "pastebin_backup_*.zip"

$files = Get-ChildItem -Path $SourcePath -File | 
    Where-Object { 
        ($_.Name -like $rarPattern -and $_.Name -match "^user-\d+-\d+\.rar$") -or 
        ($_.Name -like $zipPattern -and $_.Name -match "^pastebin_backup_\d{8}\.zip$")
    }

return $files

}

移动文件并显示进度

function Move-FilesWithProgress {
[CmdletBinding()]
param (
[Parameter(Mandatory=true)] [System.IO.FileInfo[]]Files,
[Parameter(Mandatory=true)] [string]DestinationPath
)

$totalFiles = $Files.Count
if ($totalFiles -eq 0) {
    Write-Warning "未找到符合条件的文件"
    return
}

$currentFile = 0

foreach ($file in $Files) {
    $currentFile++
    $progress = [math]::Round(($currentFile / $totalFiles) * 100, 2)
    
    Write-Progress -Activity "移动文件" `
                  -Status "正在移动: $($file.Name) ($currentFile/$totalFiles)" `
                  -PercentComplete $progress
    
    try {
        $destFile = Join-Path $DestinationPath $file.Name
        Move-Item -Path $file.FullName -Destination $destFile -Force
        Write-Host "已移动: $($file.Name)"
    }
    catch {
        Write-Error "移动文件 $($file.Name) 失败: $($_.Exception.Message)"
    }
    
    Start-Sleep -Milliseconds 100  # 短暂休眠以确保进度条更新
}

Write-Progress -Activity "移动文件" -Completed

}

主函数

function Main {
[CmdletBinding()]
param (
[Parameter(Mandatory=true)] [string]DestinationPath
)

# 获取下载文件夹
$downloadsFolder = Get-DownloadsFolder

# 验证目标路径
if (-not (Test-DestinationPath -Path $DestinationPath)) {
    exit 1
}

# 获取目标文件
$targetFiles = Get-TargetFiles -SourcePath $downloadsFolder

# 移动文件并显示进度
Move-FilesWithProgress -Files $targetFiles -DestinationPath $DestinationPath

Write-Host "移动完成!共处理 $(${targetFiles}.Count) 个文件"

}

执行脚本

destination = "D:\Backup\Files" # 目标路径变量,可根据需要修改 Main -DestinationPathdestination

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容