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

如何编写Windows批处理命令复制最新版本安装文件至目标目录?

Windows批处理实现复制最新版本安装程序

以下是实现需求的批处理脚本,可直接使用:

@echo off
setlocal enabledelayedexpansion

:: 定义源根目录与目标目录
set "source_root=C:\test\2.4"
set "target_dir=C:\install\software"

:: 自动创建目标目录(不存在时)
if not exist "%target_dir%" mkdir "%target_dir%"

:: 获取源根目录下最新的子文件夹(按名称倒序取第一个)
for /f "delims=" %%d in ('dir /b /ad /o-n "%source_root%"') do (
    set "latest_dir=%%d"
    goto :copy_files
)

:copy_files
:: 复制指定安装程序到目标目录,覆盖已存在文件
copy "%source_root%\!latest_dir!\installerA.exe" "%target_dir%" /y
copy "%source_root%\!latest_dir!\installerB.exe" "%target_dir%" /y

echo 复制完成!
endlocal

脚本说明:

  • @echo off:关闭命令回显,避免输出执行过程中的冗余命令行指令
  • setlocal enabledelayedexpansion:启用延迟环境变量扩展,保证循环内的变量能正确赋值
  • 目录定义行:将源根目录和目标目录设为变量,后续修改路径只需改动这两行即可
  • 目录创建命令:确保目标目录存在,避免复制时因目录缺失报错
  • 子文件夹获取逻辑:dir /b /ad /o-n会列出源根目录下的所有子文件夹,/o-n按名称倒序排列,取第一个即为日期最新的文件夹(因你的子文件夹名称以日期开头,字符串越大对应日期越晚)
  • 复制命令:/y参数表示直接覆盖目标目录中已存在的同名文件,无需手动确认

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 08:07:46