Endoro has a good working solution for the stated problem. Another option is to simply PUSHD to where the files are located. Then you no longer need to include the path in the remainder of the code.
Other points unrelated to the question:
It is probably a good idea to initialize counter to 0, just in case some other process already set the value to a number.
You don't really need the nname
variable.
I prefer to transfer the counter value to a FOR variable so that I don't need to use the CALL construct. (For those that don't know, the delayed expansion toggling is to protect !
characters that may be in the file name).
@echo off
setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
pushd "C:\Users\%username%\Downloads"
set /a counter=0
for /f "delims=" %%a in ('dir Export_*.csv /b /a-d /o-d') do (
set "fname=%%~a"
set /a counter+=1
setlocal enableDelayedExpansion
for %%N in (!counter!) do (
endlocal
ren "!fname!" "!name%%N!.csv"
)
)
popd
Finally, FINDSTR with the /N option can eliminate the need for CALL or additional FOR
@echo off
setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
pushd "C:\Users\%username%\Downloads"
for /f "tokens=1* delims=:" %%A in (
'dir Export_*.csv /b /a-d /o-d ^| findstr /n "^"'
) do (
set "fname=%%~B"
setlocal enableDelayedExpansion
ren "!fname!" "!name%%A!.csv"
endlocal
)
popd