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

求助:使用批处理变量复制特定格式.tst文件及对应文件夹

批量复制符合特定格式的.tst文件(保留文件夹结构)

需求匹配分析

你需要筛选的文件需同时满足:

  • 后缀为.tst
  • 文件名结构严格符合:Axxxx_ZZ[4字母+3数字|5字母+4数字]_DTx_Fx.tst
    • Axxxx:A开头+4位数字
    • ZZ后的部分:要么是4个字母+3个数字,要么是5个字母+4个数字
    • DTx_Fx:DT+1位数字,下划线后F+1位数字

下面给你两种适合新手的实现方案,均无需额外安装工具,直接在Windows环境下运行:


方案1:PowerShell脚本(精准匹配,推荐)

PowerShell支持正则表达式,能精准匹配你要的文件格式,且自动保留文件夹结构。

  1. 打开PowerShell(按下Win+X,选择「Windows PowerShell」或「终端」)
  2. 复制以下代码,修改开头的$sourcePath(C盘的源路径)和$targetPath(X盘的目标路径):
# 定义源文件夹和目标文件夹路径
$sourcePath = "C:\Your\Source\Directory"
$targetPath = "X:\Your\Target\Directory"

# 正则表达式,精准匹配你需要的文件名格式
$filePattern = '^A\d{4}_ZZ(?:[A-Za-z]{4}\d{3}|[A-Za-z]{5}\d{4})_DT\d_F\d\.tst$'

# 遍历源文件夹下所有.tst文件,匹配正则
Get-ChildItem -Path $sourcePath -Filter "*.tst" -Recurse | Where-Object {
    $_.Name -match $filePattern
} | ForEach-Object {
    # 计算目标文件的路径,保留原文件夹结构
    $relativePath = $_.FullName.Substring($sourcePath.Length)
    $destination = Join-Path -Path $targetPath -ChildPath $relativePath

    # 创建目标文件夹(如果不存在)
    New-Item -ItemType Directory -Path (Split-Path $destination -Parent) -Force | Out-Null

    # 复制文件
    Copy-Item -Path $_.FullName -Destination $destination -Force
}

Write-Host "文件复制完成!"
  1. 粘贴到PowerShell窗口,按下回车运行即可。

方案2:批处理脚本(简单易读)

如果你更习惯用命令提示符,也可以用批处理脚本,虽匹配精度稍弱,但能覆盖你的需求:

  1. 打开记事本,复制以下内容,修改开头的source和target路径:
@echo off
setlocal enabledelayedexpansion

:: 定义源路径和目标路径
set "source=C:\Your\Source\Directory"
set "target=X:\Your\Target\Directory"

:: 遍历所有.tst文件,按格式筛选
for /r "%source%" %%f in (A????_ZZ???????_DT?_F?.tst A????_ZZ????????_DT?_F?.tst) do (
    :: 提取文件名,检查ZZ后的部分长度(4+3=7位,5+4=9位)
    set "filename=%%~nf"
    for /f "tokens=2 delims=_" %%p in ("!filename!") do (
        set "zzPart=%%p"
        if "!zzPart:~2,7!" neq "" if "!zzPart:~9!" equ "" (
            :: 保留文件夹结构,创建目标文件夹
            set "relativePath=%%~dpf"
            set "relativePath=!relativePath:%source%=!"
            md "%target%!relativePath!" 2>nul
            :: 复制文件
            copy "%%f" "%target%!relativePath!" /y
        )
    )
)

echo 文件复制完成!
pause
  1. 保存文件为CopyFiles.bat(注意保存类型选「所有文件」)
  2. 双击运行这个批处理文件即可。

注意事项

  • 确保你对源路径有读取权限,对目标路径有写入权限
  • 如果目标路径不存在,脚本会自动创建
  • 测试时可以先找几个样本文件运行,确认筛选结果正确后再处理1万+文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:45:43