Search code examples
batch-filebatch-rename

Script to batch rename based on first character of original filename?


we are OCRing all our sales order in our office. Pdf files being Created based on captured Data. The files looks like these S123456.pdf, S239463.pdf. some times the OCR software "reads" the "S" as a "5". (files end up created as 5123456.pdf.) I am looking for a Batch file for a windows command prompt environment that would rename only the files with the first character that starts with "5", rename it the "S" and leaving the rest intact. preferred to it apply to all sub-folders. I google around tried to modify some examples.. Can't get them working.. Please help!


Solution

  • This should work - it will only echo the ren commands to the screen so if it looks ok then remove the echo

    @echo off
    setlocal enabledelayedexpansion
    for /r %%a in (5*.pdf) do (
    set "name=%%~nxa"
    echo ren "%%a" "S!name:~1!
    )