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

Batch脚本问题:如何提取JPG文件名中的首个4位数字?

提取JPG文件名中第一个4位数字的批处理解决方案

我的文件夹中有一些.jpg文件,想要提取文件名中的第一个4位数字。例如:photo.20.2023-beach.jpg,我希望提取出2023,但我尝试的所有方法得到的都是2020...我对batch批处理了解甚少,通常依靠ChatGPT编写基础代码,一般都能达成目标,但这次我卡住了。

原尝试代码

@echo off
setlocal enabledelayedexpansion

rem Set the current directory
set "directory=%cd%"

rem Iterate through all .jpg files in the directory
for %%F in ("%directory%\*.jpg") do (
    rem Extract the first four-digit number from the filename
    set "filename=%%~nF"
    set "digits="
    
    rem Loop through each character in the filename
    for /l %%I in (0, 1, 255) do (
        set "char=!filename:~%%I,1!"

        rem Check if the character is a digit
        if "!char!" geq "0" if "!char!" leq "9" (
            set "digits=!digits!!char!"

            rem Check if four consecutive digits are found
            if defined digits (
                set "firstDigit=!digits:~0,1!"
                set "fourthDigit=!digits:~3,1!"
                if defined fourthDigit (
                    if !firstDigit! neq "0" (
                        echo File: %%F, First four-digit number: !digits!
                        goto :found_digits
                    ) else (
                        set "digits=!fourthDigit!"
                    )
                )
            )
        ) else (
            set "digits="
        )
    )

    rem Check if digits were found
    :found_digits
    if not defined digits (
        echo File: %%F, No four-digit number found
    )
)

问题分析

原代码的核心逻辑错误在于:

  1. 收集数字时未限制长度,出现超过4位的数字串时会错误输出冗余内容
  2. 处理首位为0的4位数字时,错误将digits重置为第四位字符,破坏后续数字收集流程
  3. goto :found_digits的跳转逻辑会打乱文件循环的执行顺序,无法正确处理多个文件

修正后的代码

@echo off
setlocal enabledelayedexpansion

rem 遍历当前目录下的所有jpg文件
for %%F in ("*.jpg") do (
    set "filename=%%~nF"
    set "temp_digits="
    set "result="
    
    rem 遍历文件名的每个字符
    for /l %%I in (0, 1, 255) do (
        set "char=!filename:~%%I,1!"
        if "!char!"=="" goto :end_char_loop
        
        rem 判断是否为数字
        if "!char!" geq "0" if "!char!" leq "9" (
            set "temp_digits=!temp_digits!!char!"
            rem 只保留最近的4位数字,避免冗余
            if "!temp_digits:~4!" neq "" set "temp_digits=!temp_digits:~-4!"
            
            rem 检查是否凑够4位,且第一位非0
            if "!temp_digits:~3,1!" neq "" (
                if "!temp_digits:~0,1!" neq "0" (
                    set "result=!temp_digits!"
                    goto :end_char_loop
                )
            )
        ) else (
            set "temp_digits="
        )
    )
    
:end_char_loop
    if defined result (
        echo 文件:%%F,提取到的第一个4位数字:!result!
    ) else (
        echo 文件:%%F,未找到有效4位数字
    )
)
endlocal

代码说明

  • 用temp_digits存储连续数字,长度超过4时自动截断为最后4位,确保只跟踪最近的连续数字段
  • 一旦temp_digits凑够4位且首位不为0,立即记录结果并终止字符遍历,保证提取的是第一个符合要求的4位数字
  • 通过判断char为空终止字符循环,避免无效遍历
  • 用end_char_loop标签统一处理文件结果输出,流程更清晰可控

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:42:03