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

基于文件名/序列号片段的跨目录文件复制脚本问题求助

批处理文件复制脚本问题解决指导

我正在编写一个多功能脚本,当前核心需求是根据用户输入的序列号片段,将指定目录中匹配的文件复制到目标目录。比如输入"###"并选择源文件夹X/Y后,要把文件夹内所有Test_1_051724###.txt这类文件复制到目录B。最终目标是从以太网连接的Linux主机获取文件,但现在得先解决本地复制的问题。

当前脚本输入"test"能正常创建目录并复制,但输入数字时无法完成复制,尝试了两种方法都有问题:


第一种方法:运行报错

脚本代码:

@echo off
:start

rem Prompt the user for the search string
set /p search_string=Enter the search string: 

rem Prompt the user to choose the source directory
echo Choose the source directory:
echo 1. EE1
echo 2. EE2
set /p source_dir_choice=Enter 1 or 2: 


rem Set the source directory based on the user's choice
if %source_dir_choice% == 1 (
    set source_dir="C:\Users\cliljohn\Documents\DCD\EE1\."
    set dest_dir="C:\Users\cliljohn\Documents\DCD\EE1\TestMove1"
) else if %source_dir_choice% == 2 (
    set source_dir="C:\Users\cliljohn\Documents\DCD\EE1\."
    set dest_dir="C:\Users\cliljohn\Documents\DCD\EE1\TestMove2"
) else (
    echo Invalid choice. Exiting.
    goto end
)



@REM rem destination directory
@REM if %source_dir_choice% == 1 (
@REM     set dest_dir=C:\Users\cliljohn\Documents\DCD\EE1\TestMove1
@REM ) else if %source_dir_choice% == 2 (
@REM     set dest_dir=C:\Users\cliljohn\Documents\DCD\EE1\TestMove2
@REM ) else (
@REM     echo Invalid choice. Exiting...
@REM     goto start
@REM )


rem Create the destination directory if it doesn't exist
if not exist "%dest_dir%" mkdir "%dest_dir%"

rem Copy the files that contain the search string
for %%f in ("%source_dir%\*") do (
    if "%%~nxf" gtr "" if "%%~nxf" lss "" if "%%~nxf" contains "%search_string%" (
        copy "%%f" "%dest_dir%"
        echo Copied file: %%~nxf
    )
)

echo.
echo File copy complete.
echo.

goto start

:end

问题分析:

  • source_dir路径错误,多余的\.属于无效写法
  • 批处理原生不支持contains语法,无法用它判断字符串包含关系
  • gtr ""和lss ""属于无效逻辑,对判断无任何帮助

第二种方法:仅匹配文件名开头

脚本代码:

@echo off
:start

rem Prompt the user for the partial file name
set /p partial_filename=Enter the partial file name: 

rem Prompt the user to choose the source directory
echo Choose the source directory:
echo 1. C:\Users\cliljohn\Documents\DCD\EE1
echo 2. C:\Users\cliljohn\Documents\DCD\EE2
set /p source_dir_choice=Enter 1 or 2: 

rem Set the source directory based on the user's choice
if %source_dir_choice% == 1 (
    set source_dir=C:\Users\cliljohn\Documents\DCD\EE1
) else if %source_dir_choice% == 2 (
    set source_dir=C:\Users\cliljohn\Documents\DCD\EE2
) else (
    echo Invalid choice. Exiting.
    goto end
)

rem Set the destination directory
set dest_dir=C:\Users\cliljohn\Documents\DCD\EE1\TestMove

rem Create the destination directory if it doesn't exist
if not exist "%dest_dir%" mkdir "%dest_dir%"

rem Copy the files that match the partial file name
for %%f in ("%source_dir%\%partial_filename%*") do (
    copy "%%f" "%dest_dir%"
    echo Copied file: %%~nxf
)

echo File copy complete.


@REM goto start
:end

问题分析:

  • 使用%partial_filename%*的通配规则只能匹配以输入内容开头的文件名,无法匹配中间包含输入片段的文件

修正后的完整脚本

以下是解决了上述所有问题的脚本,支持匹配文件名任意位置的输入片段,且数字输入正常:

@echo off
setlocal enabledelayedexpansion
:start

rem 提示用户输入搜索片段
set /p search_string=Enter the search string: 

rem 提示选择源目录
echo Choose the source directory:
echo 1. EE1
echo 2. EE2
set /p source_dir_choice=Enter 1 or 2: 

rem 设置源目录和目标目录
if %source_dir_choice% == 1 (
    set "source_dir=C:\Users\cliljohn\Documents\DCD\EE1"
    set "dest_dir=C:\Users\cliljohn\Documents\DCD\EE1\TestMove1"
) else if %source_dir_choice% == 2 (
    set "source_dir=C:\Users\cliljohn\Documents\DCD\EE2"
    set "dest_dir=C:\Users\cliljohn\Documents\DCD\EE1\TestMove2"
) else (
    echo Invalid choice. Exiting.
    goto end
)

rem 创建目标目录(不存在则创建)
if not exist "%dest_dir%" mkdir "%dest_dir%"

rem 遍历源目录所有文件,匹配包含搜索片段的文件
for %%f in ("%source_dir%\*") do (
    set "filename=%%~nxf"
    rem 判断文件名是否包含搜索字符串:替换后字符串不同则说明包含
    if not "!filename:%search_string%=!" == "!filename!" (
        copy "%%f" "%dest_dir%" >nul
        echo Copied file: %%~nxf
    )
)

echo.
echo File copy complete.
echo.

goto start

:end
endlocal

关键改进点:

  1. 启用setlocal enabledelayedexpansion,支持在for循环中动态处理变量
  2. 使用!filename:%search_string%=!的字符串替换语法,判断文件名是否包含目标片段
  3. 修正源目录路径错误,去掉多余的无效字符
  4. 采用set "var=value"的规范变量赋值写法,避免空格和引号问题
  5. 复制时添加>nul屏蔽系统默认输出,只保留自定义提示信息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 03:28:12