You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于含3万条记录的TXT文件批量移动指定路径文件?

嘿,这个批量移动文件的需求很常见,我给你准备了几个不同工具的解决方案,你可以根据自己熟悉的技术栈来选:

方案1:Windows 批处理脚本(适合熟悉CMD的用户)

如果你习惯用Windows命令提示符,这个脚本简单直接,不需要额外安装工具:

@echo off
setlocal enabledelayedexpansion

:: *请替换成你实际的列表文件路径*
set "LIST_FILE=C:\your\path\filelist.txt"

for /f "tokens=1,* delims=to" %%a in (%LIST_FILE%) do (
    :: 去除源路径末尾的空格
    set "SOURCE=%%a"
    set "SOURCE=!SOURCE:~0,-1!"
    :: 去除目标路径开头的空格
    set "DEST=%%b"
    set "DEST=!DEST:~1!"
    
    :: 检查源文件是否存在
    if exist "!SOURCE!" (
        :: 自动创建不存在的目标文件夹
        if not exist "!DEST!" mkdir "!DEST!"
        :: 执行移动操作
        move "!SOURCE!" "!DEST!"
        echo 已完成: !SOURCE! -^> !DEST!
    ) else (
        echo 警告: 文件不存在 - !SOURCE!
    )
)

echo 所有文件处理完毕!
pause

使用步骤:

  • 把上面的代码复制到记事本,修改LIST_FILE为你的txt文件路径
  • 保存为move_files.bat,双击运行即可
方案2:PowerShell脚本(更灵活,适合复杂路径场景)

PowerShell处理带空格的路径更省心,功能也更强大:

# *请替换成你实际的列表文件路径*
$listPath = "C:\your\path\filelist.txt"

Get-Content $listPath | ForEach-Object {
    # 按" to "分割成源路径和目标路径(只分割1次,避免路径含"to"字符)
    $parts = $_ -split " to ", 2
    if ($parts.Count -eq 2) {
        $source = $parts[0].Trim()
        $dest = $parts[1].Trim()
        
        if (Test-Path $source -PathType Leaf) {
            # 自动创建目标文件夹(如果不存在)
            if (-not (Test-Path $dest)) {
                New-Item -ItemType Directory -Path $dest | Out-Null
            }
            # 移动文件,-Force参数可覆盖目标已存在的同名文件
            Move-Item -Path $source -Destination $dest -Force
            Write-Host "已完成: $source -> $dest"
        } else {
            Write-Warning "跳过不存在的文件: $source"
        }
    } else {
        Write-Warning "跳过格式错误的行: $_"
    }
}

Write-Host "所有处理完成!"

使用步骤:

  • 复制代码到记事本,修改$listPath为你的txt路径
  • 保存为move_files.ps1,右键选择“用PowerShell运行”
方案3:Python脚本(跨平台,适合熟悉编程的用户)

如果需要跨Windows/macOS/Linux使用,或者想自定义更多逻辑,Python脚本是不错的选择:

import os
import shutil

# *请替换成你实际的列表文件路径*
list_file = r"C:\your\path\filelist.txt"

with open(list_file, 'r', encoding='utf-8') as f:
    for line_num, line in enumerate(f, 1):
        line = line.strip()
        if not line:
            continue
        
        # 检查行格式是否正确
        if " to " not in line:
            print(f"第{line_num}行格式错误,跳过: {line}")
            continue
        
        source, dest = line.split(" to ", 1)
        source = source.strip()
        dest = dest.strip()
        
        if os.path.isfile(source):
            # 自动创建目标文件夹(包括嵌套子文件夹)
            os.makedirs(dest, exist_ok=True)
            try:
                shutil.move(source, dest)
                print(f"已完成: {source} -> {dest}")
            except Exception as e:
                print(f"第{line_num}行移动失败 {source}: {str(e)}")
        else:
            print(f"第{line_num}行: 文件不存在 {source}")

print("所有文件处理完毕!")

使用步骤:

  • 确保已安装Python环境
  • 修改list_file为你的txt路径,保存为move_files.py
  • 在终端运行python move_files.py即可

内容的提问来源于stack exchange,提问作者Robin Gui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:43:27