@ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN (
'dir /b /a-d *_*.7k ^|findstr /v /b /l "g_x_" '
) DO (
REN "%%a" "g_x_%%a"
)
GOTO :EOF
The dir
finds filenames that match *_*.7k
and this is piped to a FINDSTR
which allows through only those names that do NOT match (/v
) the literal (/l
) string "g_x_"
at the beginning (/b
). The caret (^
) before the pipe is required to tell CMD that the pipe is part of the command to be executed by the for
, not a parameter to the dir
.
The resultant filenames are then assigned to %%a
, prepended and renamed
This approach first makes a list of the existing filenames, then processes that list. If an attempt is made to rename in-place, then it's possible that a file aaa_bbb.7k would be renamed to g_x_aaa_bbb.7k and then again to g_x_g_x_aaa_bbb.7k...