Travis R
Travis R

Reputation: 27

Renaming files after a specific character

Was hoping to get some help here. I have a bunch of files that are monitored and the ending facility removed, files look as follows:

LastName, FirstName_DOS-Facility.pdf

I currently run the below:

@echo off

for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf"') do (
    ECHO move "%%a-%%b" "%%a%%~xb"
)

And it creates LastName, FirstName_DOS.pdf

The issue I'm running into is multiple files with the same name, my batch file just replaces the older file with the newer. Is there a way to append a _1.pdf _2.pdf _3.pdf , etc if needed? Thank you for the help!

Upvotes: 0

Views: 650

Answers (2)

sambul35
sambul35

Reputation: 1098

Save the script to test.bat and run from open Cmd Prompt. Replace dir value with your path. Let me know if any errors.

@echo off
setlocal enabledelayedexpansion
set "dir=C:\My_Files"
pushd "%dir%"
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.pdf" 2^>nul') do (
    call :rename %%a %%b )
popd
exit /b

:rename
set "i="
:loop
if exist "%1!i!%~x2" (set /a "i+=1" & goto :loop)
ren "%1-%2" "%1!i!%~x2"
exit /b

Upvotes: 0

Mofi
Mofi

Reputation: 49196

Here is a commented batch code suitable for your task requirements.

@echo off
setlocal EnableExtensions EnableDelayedExpansion

for /F "tokens=1* delims=-" %%a in ('dir "*-*.pdf" /A-D /B 2^>nul') do (
    if not exist "%%a%%~xb" (
        ren "%%a-%%b" "%%a%%~xb"
    ) else (
        call :GetNextAppendix "%%a" "%%~xb"
        ren "%%a-%%b" "%%a!NextAppendix!%%~xb"
    )
)

endlocal
goto :EOF

rem This subroutine inserts between file name passed as first argument
rem and file extension passed as second argument an underscore and an
rem incrementing number in range 1 to 50000 and checks if a file with
rem this name already exists. If there is no file with current number
rem in file name, this number with the preceding underscore is assigned
rem to an environment variable used in parent process routine to rename
rem the file.

:GetNextAppendix
for /L %%I in (1,1,50000) do (
    if not exist "%~1_%%I%~2" (
        set "NextAppendix=_%%I"
        goto :EOF
    )
)

rem Windows command interpreter should never reach this part of the code.
rem But in case of this really happens, simply clear the appendix variable
rem which results in a failed renaming of file in parent process loop 
rem above with output of an error message because of file already existing.

set "NextAppendix="
goto :EOF

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • ren /?
  • set /?
  • setlocal /?

And see also the Microsoft article about Using command redirection operators for an explanation of 2^>nul which is 2>nul with redirection operator > escaped with ^ to be applied on execution of command DIR and not being interpreted as redirection operation for command FOR at an invalid position within the command line.

This redirects the error message output by DIR on no file matching the wildcard pattern *-*.pdf from STDERR to device NUL to suppress it.

Upvotes: 1

Related Questions