随笔 - 55  文章 - 0  评论 - 4  阅读 - 39841

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   kelisi_king  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示