Windows将子目录中指定类型文件移动到另外的指定目录

前提

迅雷下载的视频文件(电视剧)被分散到了各个目录中,想要观看还得一个目录一个目录的打开,或者手动拷贝,非常繁琐。

需求

Windows powershell脚本递归指定目录及子目录下的视频文件并将超过100M大小的视频移动到另一个目录中。

Action

kimi thinking....

结果

# 定义源目录和目标目录
$sourceDir = 'D:\Life\movie\鹊刀门传奇'  # 替换为你的源目录路径,确保路径正确
$targetDir = 'D:\Life\movie\鹊刀门传奇2'  # 替换为你的目标目录路径,确保路径正确

# 确保目标目录存在
if (-not (Test-Path -Path $targetDir)) {
    New-Item -ItemType Directory -Path $targetDir
}

# 遍历源目录及其子目录中的所有视频文件
Get-ChildItem -Path $sourceDir -Recurse -Filter *.mp4 | ForEach-Object {
    # 检查文件大小是否超过 100MB
    if ($_.Length -gt 100MB) {
        # 构造目标文件路径
        $targetFilePath = Join-Path -Path $targetDir -ChildPath $_.Name
        # 检查目标文件是否已存在
        if (-not (Test-Path -Path $targetFilePath)) {
            # 使用 -LiteralPath 参数来避免通配符问题
            Move-Item -LiteralPath $_.FullName -Destination $targetFilePath
            Write-Output "Moved: $($_.FullName) to $targetFilePath"
        } else {
            Write-Output "File already exists: $targetFilePath"
        }
    }
}

执行

  1. 将上面的脚本拷贝到新建文件中并保存为UTF-8 With Bom(包含bom头的UTF-8编码的)CopyLargeVideo.ps1文件。重要带bom头的UTF8编码!!!
  2. 打开powershell,cd到脚本目录./CopyLargeVideo.ps1执行
posted on 2025-02-18 21:30  kelisi_king  阅读(47)  评论(0)    收藏  举报