0

For some reason my code below only works if the batch file is in the same folder as the files to be renamed even though i've specified the path. When the batch file is in a different folder I receive an error saying the file can't be found. Any input on this?

@echo off&setlocal
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
for /f "delims=" %%a in ('dir C:\Users\%username%\Downloads\Export_*.csv /b /a-d /o-d') do (
    set "fname=%%~a"
    set /a counter+=1
    SETLOCAL ENABLEDELAYEDEXPANSION
    call set "nname=%%name!counter!%%"
    ren "!fname!" "!nname!%%~xa"
    endlocal
)
Cass
  • 39
  • 1
  • 6
  • Out on a limb (since I haven't used DOS in years), but betting that the directory isn't being included in the fname - you may need to specify it in the rename command as well. – cbmanica Nov 06 '13 at 22:55

3 Answers3

3

just add a working path:

@echo off&setlocal
set "workingpath=%userprofile%\Downloads"
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
for /f "delims=" %%a in ('dir "%workingpath%\Export_*.csv" /b /a-d /o-d') do (
    set "fname=%%~a"
    set /a counter+=1
    SETLOCAL ENABLEDELAYEDEXPANSION
    call set "nname=%%name!counter!%%"
    ren "%workingpath%\!fname!" "!nname!%%~xa"
    endlocal
)
Endoro
  • 37,015
  • 8
  • 50
  • 63
  • +1, But It should be `call set "nname=%%fname%%!counter!"`, or better yet `set "nname=!fname!!counter!"`. See [my anser](http://stackoverflow.com/a/19826121/1012053). – dbenham Nov 07 '13 at 01:23
  • @dbenham excuse me, but your solutions doesn't work. I do not have a variable `name`, but I have `name1`, `name2` and so on. `nname` doesn't depend on `fname` but on `name1`, `name2` ... and it needs the `.csv` extension. – Endoro Nov 07 '13 at 06:55
  • Yes, I completely missed the point of nameX at first. I've rewritten my answer. – dbenham Nov 07 '13 at 12:37
2

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
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • In general it might be misleading to expand for loop parameters like `%%a` in `delayed expansion` (your first solution). I miss the `nameX` variables here. – Endoro Nov 07 '13 at 07:02
  • @Endoro - Look at my original solution before edits, and you will see that I ENDLOCAL out of delayed expansion before expanding `%%a`. But I rewrote the answer with correct solutions once I finally understood the purpose of the nameX variables. Thanks. – dbenham Nov 07 '13 at 12:36
  • Thanks @dbenham this worked as well! I wish I could mark them both as answers – Cass Nov 07 '13 at 15:30
1

@cbmanica is right: the directory is not being included in the variable fname, so you'll have to specify it manually in the ren command.

@echo off
setlocal ENABLEDELAYEDEXPANSION
set "name1=Bart"
set "name2=Carl"
set "name3=Judy"
set "dir=C:\Users\%username%\Downloads\"
for /f "delims=" %%a in ('dir %dir%Export_*.csv /b /a-d /o-d') do (
    set "fname=%%~a"
    set /a counter+=1
    :: <Comment> In the below line is the use of "call" necessary? </Comment>
    call set "nname=%%name!counter!%%"
    ren "!dir!!fname!" "!dir!!nname!%%~xa"
)
endlocal

That should do exactly what you want.

Monacraft
  • 6,510
  • 2
  • 17
  • 29