It is possible to obtain the required output without using an explicit counter:
@ECHO OFF
FOR /F "delims=: tokens=1*" %%R IN ('DIR /A-D-H /B /S D:\to_delete\* ^| FINDSTR /N .') DO (
ECHO %%~dpnS_%%R%%~xS
)
The DIR
command provides the list of files, FINDSTR
supplies it with numbers, the FOR /F
loop processes thee output of FINDSTR
to separate numbers from file names and ECHO
produces the final output where numbers are appended to the file names.
Note that the numbers get appended to the names, i.e. before the extensions, so that a file name like this:
D:\path\name.ext
changes to something like this:
D:\path\name_1.ext
If you actually want it to be like this:
D:\path\name.ext_1
replace the ECHO
command above with the following:
ECHO %%S_%%R