# Windows script to run as a batch to replace character in list of file names

I have 4 files as below:

C:\Documents and Settings\vm\AC R\test

JE.BOS.20130516.CSV
JE.BES.20130516.CSV
KFROPN.SAVEFILE.CSV


I have to replace "." in the filename with "-". But the extension .CSV should remain same.

1) I executed the below command and i got the error as below:

for /f "tokens=1*delims=." %%i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"


Error:

C:\Documents and Settings\vm\AC R\test>for /f "tokens=1*delims=." %%
i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"
%%i was unexpected at this time.


2) I removed one % before i and executed as below:

for /f "tokens=1*delims=." %i in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"


and got the error as below:

C:\Documents and Settings\vmeruga\ACCESS RECS\test>for /f "tokens=1*delims=." %i
in ('dir /a-d/b *.*.CSV') do ren "%%~i.%%~j" "%%~i-%%~j"

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%JEFFERIES.%BODPOS.20130
516.CSV" "%JE-%BODPOS.20130516.CSV"
The system cannot find the file specified.

The system cannot find the file specified.

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBAROPN.%SAVEFILE.CSV"
"%KFOPN-%SAVEFILE.CSV"
The system cannot find the file specified.

C:\Documents and Settings\vmeruga\ACCESS RECS\test>ren "%KFBARTRD.%SAVEFILE.CSV"
"%KFBARTRD-%SAVEFILE.CSV"
The system cannot find the file specified.

-
I believe that your first try should work if you put it into a batch file. To run it interactively, change all occurrences of %% to %. –  Scott May 28 '13 at 18:19
@Scott: He got the first command from an answer to his previous question. It's clearly incorrect for the filenames provided here and will only replace the first period, try it and see. –  Karan May 28 '13 at 19:51
@Karan: «I have to replace "." in the filename with "-".» OK, I suppose that could mean that he wants to replace all periods (before the extension). But I submit that the question is ambiguous. I interpreted it to mean that the first period was the only one he wanted to change, and the OP doesn’t say anything to clarify that. –  Scott May 28 '13 at 21:24
@Scott: Well, I'm assuming he posted this since the solution that does replace the first period did not do what he wanted. Still, you're right, a bit of clarification wouldn't hurt. –  Karan May 28 '13 at 21:36
I believe “the solution that does replace the first period did not do what he wanted” because he typed it interactively with %%i instead of %i, etc… –– see the error message he reported. –  Scott May 28 '13 at 21:43

Here you go, save the following as a .BAT file and execute:

@setlocal enabledelayedexpansion&&for /f %%a in ('dir /b *.csv') do @set fn=%%~na&&set fn=!fn:.=-!&&ren "%%a" "!fn!%%~xa"

-
Thanks Karan. It worked perfectly as i expected. –  user225817 May 29 '13 at 7:57
Karan. When i run this script from test directory it works. What if would like to run it from any directory/path. I need to mention the path name in the script right!. Can you please tell me where do i modify the script to specify the path as "c:\test\" –  user225817 May 29 '13 at 8:23
Karan. I could able to do it. Please tell me how to accept the answer –  user225817 May 29 '13 at 9:15
@user225817: A simple cd /d "drive:\path\to\dir" as the first line will take you to the dir you want to run the rest of the batch in, but I guess you already figured that out. You can accept the answer by clicking the empty green check mark to the left. It will become filled on clicking. –  Karan May 29 '13 at 12:17

try this, look at the output and remove the word echo, if it is OK:

@echo off&setlocal
cd /d "C:\Documents and Settings\vm\AC R\test"
for %%i in (*.*.csv) do (
set "fname=%%~ni"
setlocal enabledelayedexpansion
set "nname=!fname:.=-!"
echo rename "!fname!.CSV" "!nname!.CSV"
endlocal
)
-
Endoro. I tried the above script putting in a batch file, I am not sure whether i should put in a batch file or run in a coomad line. As a batch file, I couldn't see the results. Thanks for your time on this issue. –  user225817 May 29 '13 at 7:59