need batch file to rename files in bulk

February 22, 2011 at 08:12:10
Specs: Windows XP
Renaming files:
I have 1000s of files named somewhat in sequence but there is an extra character I need to delete in them. Among the numbers in the file is a date which I want to keep. For renaming I usually use a simple rename command as such: rename file022011*.txt file022311*.txt to rename the files to change a date for instance. The problem is the files I want to rename are as such:
file0022011001.txt
file0022011002.txt
file0022011003.txt
file0022011006.txt

and I want to rename them as such
file022011001.txt
file022011002.txt
file022011003.txt
file022011006.txt

I want to get rid of that extra 0 after file. So using * cannot shorten the file name. I tried using ? too but that didn't work. I need something a little more sophisticated like a batch file but my knowledge of dos commands is limited.

I hope that's clear.


See More: need batch file to rename files in bulk

Report •

#1
February 22, 2011 at 10:02:19
try this one from a batch file .create a test folder same you your scnario with 4-5 files and see if it does as exactly as you want , if yes then copy that batch file into the folder which contian 1000 of files and run it.


@echo off
dir /b *.txt | findstr /v "filelist" >filelist.txt
for /f %%a in (filelist.txt) do (
echo processing %%a
for /f "tokens=1,* delims=0" %%i in ("%%a") do (
ren %%a %%i0%%j

)

)

Subhash Chandra.


Report •

#2
February 22, 2011 at 11:14:14
Very close. Did the trick with date of the format 0#/##/## (month = 01 to 09). It didn't work with the format ##/##/## (month = 10 to 12).

Report •

#3
February 22, 2011 at 11:40:00
Is that for me ?

Subhash Chandra.


Report •

Related Solutions

#4
February 22, 2011 at 12:32:42
@Subhash Chandra: yes. Your batch file worked very well except for the months 10-12

so those would be named as such:
file0120110001.txt
file0120110002.txt
file0120110003.txt


Report •

#5
February 22, 2011 at 13:17:46
i was not aware the flie name format is related to dates. You didn't mentioned so :) how i was supposed to know things which is in your mind.

please .try this ..it will take last 13 charectors from the filename and then ise it with file..which will skip the one 0 after file..

==============================================================
@echo off & setlocal enabledelayedexpansion

dir /b *.txt | findstr /v "filelist" >filelist.txt
for /f %%a in (filelist.txt) do (
echo processing file %%a
for /f "tokens=* delims=0" %%i in ("%%a") do (
set file=%%i

)
ren %%a file!file:~-13!
)

===================================================

Subhash Chandra.


Report •

#6
February 22, 2011 at 13:52:49
Thanks, Subhash! It worked like a charm. It saved me hours of manual renames.

Report •

#7
February 22, 2011 at 14:01:34
You welcome.. :(

Subhash Chandra.


Report •

Ask Question