命令提示符(Command Prompt)下特定格式文件重命名的REN命令问题咨询
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:
- 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 asFri 05/19/2022, this would output20221905instead of the desired051922. The substring positions depend on your system's regional settings, making this approach unreliable. - Wildcard Limitations: CMD's
RENcommand 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.
Solution 1: Reliable Batch Script (Recommended)
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:
wmicpulls the date directly from the system, so we don't have to rely on%date%'s variable formatting. We then format it toMMDDYY(e.g., 05/19/2022 becomes 051922). - Filename Splitting: The outer
forloop splits your filename into 5 parts using.as a delimiter:%%i: 14876128%%j: AOB%%k: A14R2229-A14S2229%%l: USER%%m: pdf
- Inner Loop: Splits
%%kinto 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

