Add a suffix to file basenames in a directory

64 Views Asked by At

I am trying to create a batch file to add the suffix _Reuploads to the basenames of all CZI files in a directory named Reuploads.

The file names in Reuploads follow the basename format AB123456_pX_s0_m0, (sometimes they follow the format AB123456_pX_s0_m0_R1, not sure if this matters?).

I found this code, and edited it, but it does not seem to work.

@for /f "delims=" %%i in ('dir "C:\Reupload" /s /b /a-d ^| findstr /v "_Reupload\.[^.]*$"') do ( ren "%%~fi" "%%~ni_Reupload%%~xi" )
1

There are 1 best solutions below

3
TristanMas On BEST ANSWER

You can try this

@echo off
setlocal enabledelayedexpansion


for /r C:\ %%d in (.) do (
    if /i "%%~nxd"=="Reuploads" (
        echo Found folder: %%d
        pushd %%d
        for %%f in (*.czi) do (
            set "filename=%%f"
            if not "!filename!"=="!filename:_Reuploads=!" (
                echo File already has suffix: %%f
            ) else (
                ren "%%f" "!filename!_Reuploads.czi"
                echo Renamed: %%f
            )
        )
        popd
    )
)

echo.
echo Renaming complete.
pause

But it may be slow because it looks in all the computer for the "reuplods" folder.