Solved Rename selected CSV files from a folder and rename

October 4, 2012 at 16:30:00
Specs: Windows 7
I have a folder with multiple CSV files, I want to use a batch file to rename selected ones.

ABC_Company_1-2-2012.csv, ABC_Company_2-2-2012.csv, ABC_Company_3-2-2012.csv,
XYZ_Company_1-2-2012.csv, XYZ_Company_2-2-2012.csv, XYZ_Company_3-2-2012.csv,
MNO_Company_1-2-2012.csv, MNO_Company_2-2-2012.csv, MNO_Company_3-2-2012.csv, etc.

I only want to rename the ones with ABC in the name... to ABC1.csv, ABC2.csv, ABC3.csv.

Is this possible?

Thanks.


See More: Rename selected CSV files from a folder and rename

Report •


#1
October 4, 2012 at 17:13:29
::===== begin
for /f "tokens=1-3* delims=_-" %%a in ('dir /b abc_*.csv') do @echo ren %%a_%%b_%%c-%%d.csv %%a%%c.csv
::===== end

safety is ON for crash-test. To activate destructive potential, delete "@echo" from the code. Somewhat tested, but never trust someone elses code on real data...


Report •

#2
October 4, 2012 at 18:40:31
Thanks nbrane. This is very close.

The command that results from the code is:

ren ABC_Company_1-2-2012.csv.csv ABC1.csv

I can't figure out why there is the additional .csv at the end of the file in the command?


Report •

#3
October 4, 2012 at 19:21:42
✔ Best Answer
Sorry, my mistake. The %%d Included the .csv extension, so I should have left that off, instead, I doubled it. here is fix (might work):

for /f "tokens=1-3* delims=_-" %%a in ('dir /b abc_*.csv') do @echo ren %%a_%%b_%%c-%%d %%a%%c.csv


Report •

Related Solutions

#4
October 4, 2012 at 20:29:59
Thanks! it worked great.

Report •

Ask Question