admin管理员组

文章数量:1125603

I have this batch script but it is not renaming the files at all.

I have a list of documents that have been named in this format: yyyymmdd_docname1.pdf. I am trying to rename all of the documents to docname1_yyyymmdd.pdf.

Example, to rename:

  • 20230101_fullbloodcount.pdf to fullbloodcount_20230101.pdf.

  • 20230202_esr.pdf to esr_20230202.pdf

What is constant for all these documents is the date format that is appended in front of the document name.

The script is as per below. I would appreciate if you could look over the script and let me know what is wrong with it. Thank you in advance.

@echo off
setlocal enabledelayedexpansion

set "directory=C:\Users\user1\Downloads\Rename"

cd /d "%directory%"

for %%f in (??????_*.pdf) do (
   
    set "filename=%%~nf"
    set "extension=%%~xf"
 
    set "datepart=!filename:~0,8!"
    set "docpart=!filename:~9!"
 
    set "newname=!docpart!_!datepart!!extension!"
 
    ren "%%f" "!newname!"
)

echo Files have been renamed.
pause

I have this batch script but it is not renaming the files at all.

I have a list of documents that have been named in this format: yyyymmdd_docname1.pdf. I am trying to rename all of the documents to docname1_yyyymmdd.pdf.

Example, to rename:

  • 20230101_fullbloodcount.pdf to fullbloodcount_20230101.pdf.

  • 20230202_esr.pdf to esr_20230202.pdf

What is constant for all these documents is the date format that is appended in front of the document name.

The script is as per below. I would appreciate if you could look over the script and let me know what is wrong with it. Thank you in advance.

@echo off
setlocal enabledelayedexpansion

set "directory=C:\Users\user1\Downloads\Rename"

cd /d "%directory%"

for %%f in (??????_*.pdf) do (
   
    set "filename=%%~nf"
    set "extension=%%~xf"
 
    set "datepart=!filename:~0,8!"
    set "docpart=!filename:~9!"
 
    set "newname=!docpart!_!datepart!!extension!"
 
    ren "%%f" "!newname!"
)

echo Files have been renamed.
pause
Share Improve this question edited 2 days ago Santiago Squarzon 59k5 gold badges21 silver badges50 bronze badges asked 2 days ago name_hiddenname_hidden 1 New contributor name_hidden is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 8
  • 1 What issues are you currently facing (ie. what happens when you run the script), and why is the question tagged with powershell? – Mathias R. Jessen Commented 2 days ago
  • 1 The first obvious issue is that you are using ?????? to represent yyyymmdd. The ? wildcard means zero or one character, so six of them will only match up to six characters, not the eight you want to target. – Compo Commented 2 days ago
  • There could be used the single command line: @pushd "%USERPROFILE%\Downloads\Rename" && ((@for /F "delims=" %%G in ('dir ????????_*.pdf /A-D-L /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "^[12][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]_"') do @for /F "tokens=1* delims=_" %%I in ("%%~nG") do @ren "%%G" "%%J_%%I%%~xG") & popd) That command line works also for PDF files with one or more ! in file name. The batch file with this command line can be run multiple times on same folder even on files have been renamed already before with same batch file. – Mofi Commented 2 days ago
  • Please note that a file like 20230202___esr.pdf is renamed to esr_20230202.pdf and not to __esr_20230202.pdf, i.e. the sequence of underscores between date and rest of the file name is interpreted like a single underscore which can be good or not good depending on your requirements for such a file name. This command line in the batch file would works also on drives with FAT32 and exFAT although %USERPROFILE% expands usually to a path on system drive which is nowadays always NTFS. However, loading first the file names list into memory and then renaming the files is always better. – Mofi Commented 2 days ago
  • Please read At which point does for or for /R enumerate the directory (tree)? It explains the reason for using a for /F loop which runs in background one more Windows Command Processor to run command dir, filter the output with findstr to avoid (not perfect) renaming files not beginning with a date but with eight other characters left to an underscore, capture the output file names list, and then rename one file after the other from the list in memory of cmd.exe. – Mofi Commented 2 days ago
 |  Show 3 more comments

1 Answer 1

Reset to default 0

Here's a basic example of one way to approach the task:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "Directory=%UserProfile%\Downloads\Rename"

PushD "%Directory%" 2>NUL || GoTo :EOF

For /F "Delims=" %%G In ('Dir /A:-D /B "????????_*.pdf" 2^>NUL') Do (
    If /I "%%~xG" == ".pdf" (
        Set "_=%%~nG"
        Set "$="
        Set /A "$=%%~nG" 2>NUL
        If Defined $ (
            SetLocal EnableDelayedExpansion
            If !$! GEq 19700101 (
                If !$! LEq 20251231 (
                    Ren "%%G" "!_:~9!!_:~8,1!!$!%%~xG"
                )
            )
            EndLocal
        )
    )
)

PopD
EndLocal
Exit /B

I have added some simple date validation to this code, (instead of relying upon up to any eight characters, or even a sequence of eight digits). The above is designed to work with all valid dates from 1970 up until the end of this year, (you would need to adjust the LEq string beyond that). It does not however exclude invalid dates between those two, (e.g.20150229 or 20240931), as doing so would be well beyond the requirements of your stated problem, and simple batch scripting.

本文标签: windows scriptingBatch script to rename files beginning with date format yyyymmddStack Overflow