tags:

views:

3038

answers:

4

I am trying to rename all the files present in a windows directory using FOR command as follows at the dos prompt:

for %1 in (.) do ren %1 test%1

E.g. This renames a file enc1.ctl to testenc1.ctl enc2.ctl to testenc2.ctl

Thats not what i want. What i want is enc1.ctl renamed to test1.ctl enc2.ctl renamed to test2.ctl

How do i do that?

-AD

A: 

@Akelunuk: Thanks, that w kind of works but i have files names as

h263_enc_random_pixels_1.ctl , h263_enc_random_pixels_2.ctl which i want to rename to

test1.ctl and test2.ctl respectively

Then how?

-AD

goldenmean
+1  A: 

If you know the number of files, (say 10), you can use

for /L %1 in (1,1,10) do ren enc%1.ctl test%1.ctl
David Nehme
+1  A: 

I've got it!

for %1 in (.) do ren %1 t%1

and then:

ren tenc*.* test*.*
akalenuk
A: 

I am not sure if it is possible in batch, but then again I never mastered this primitive language... :-P

If CMD isn't mandatory, but you can't use a good file renamer, you can do that with WSH:

var path= "E:/tmp";

var fso = WScript.CreateObject("Scripting.FileSystemObject");
var folder = fso.GetFolder(path);
var files = new Enumerator(folder.files);
for (; !files.atEnd(); files.moveNext())
{
  var file = files.item();
  var fileName = file.Name;
  var p = /^enc(\d+)\.ctl$/.exec(fileName);
  if (p != null)
  {
    var newFileName = "test" + p[1] + ".ctl";
    // Optional feedback
    WScript.echo(fileName + " -----> " + newFileName);
    file.Move(newFileName);
  }
}

Of course, put that in a file.js
I actually tested with file.Copy(file.ParentFolder + "/SO/" + newFileName); to avoid loosing files...

HTH.

PhiLho