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

命令提示符(Command Prompt)下特定格式文件重命名的REN命令问题咨询

Fixing Your Batch File Rename Command

Let's break down why your original REN command wasn't working, and then walk through a couple of solid solutions to get your files renamed correctly.

What Was Wrong With the Original Command?

Your original attempt had two key issues:

  1. Date Formatting: The %date% substring parameters you used (%date:~10,4%%date:~7,2%%date:~4,2%) likely produced an incorrect date string. For example, if your system's date is formatted as Fri 05/19/2022, this would output 20221905 instead of the desired 051922. The substring positions depend on your system's regional settings, making this approach unreliable.
  2. Wildcard Limitations: CMD's REN command handles wildcards in a positional way, but trying to replace the - with ... while keeping the surrounding text intact is tricky with just wildcards. The original wildcard pattern didn't precisely map to the parts of your filename that needed to stay the same.

This script uses for loops to split your filenames into exact parts, and uses wmic to get a consistent date format (avoiding regional setting issues). It will work regardless of your system's date display format.

@echo off
setlocal enabledelayedexpansion

:: Get a consistent MMDDYY date stamp (matches your example: 051922)
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('wmic path win32_localtime get day^,month^,year /format:table') do (
    set "day=%%a"
    set "month=%%b"
    set "year=%%c"
    goto :date_retrieved
)
:date_retrieved
:: Add leading zeros to day/month if needed (e.g., 5 becomes 05)
if !month! lss 10 set "month=0!month!"
if !day! lss 10 set "day=0!day!"
set "date_stamp=!month!!day!!year:~2,2!"

:: Loop through all matching files and rename them
for /f "tokens=1-5 delims=." %%i in ('dir /b *.USER.pdf') do (
    :: Split the third part (A14R2229-A14S2229) into two separate parts using "-" as delimiter
    for /f "tokens=1,2 delims=-" %%x in ("%%k") do (
        ren "%%i.%%j.%%k.%%l.%%m" "%%i.%%j.%%x...%%y.!date_stamp!.%%l.%%m"
    )
)

endlocal
echo Renaming complete!

How This Works:

  • Date Handling: wmic pulls the date directly from the system, so we don't have to rely on %date%'s variable formatting. We then format it to MMDDYY (e.g., 05/19/2022 becomes 051922).
  • Filename Splitting: The outer for loop splits your filename into 5 parts using . as a delimiter:
    • %%i: 14876128
    • %%j: AOB
    • %%k: A14R2229-A14S2229
    • %%l: USER
    • %%m: pdf
  • Inner Loop: Splits %%k into two parts using - as a delimiter, so we can replace - with ... while keeping the surrounding text intact.

Solution 2: Simplified REN Command (For Fixed Filename Lengths)

If your filenames always follow the exact length pattern (########.###.########-########.####.pdf), you can use a simplified REN command—but only if your system's date format matches the expected substring positions.

First, set the correct date stamp for your system. For example, if your %date% is Fri 05/19/2022, use:

set "date_stamp=%date:~4,2%%date:~7,2%%date:~12,2%"

Then run the rename command:

REN "########.###.########-########.####.pdf" "########.###.########...########.%date_stamp%.####.pdf"

⚠️ Note: This will fail if any part of your filename varies in length, or if your system uses a different date format (e.g., DD/MM/YYYY). Use Solution 1 for a robust, universal fix.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 21:27:28