By l0gix, April 2, 2007, 3:31 pm o'clock

 

Using FOR to Rename Files from Command Line

Although I mess around with DOS batch files and the command line, I’ve never really gotten into the FOR command. Wow, what a powerful little puppy that is. I read a post at LifeHacker today that has inspired me to figure out how to use the FOR command to handle a little housekeeping task that I’ve never figured out how to do. Until today.
And even if you don’t need to rename files, there may be some other housekeeping chores that this can help you with. Actually, the FOR command is really quite powerful; it can even look into files, which opens up all sorts of possibilities for use.

Setup

Let’s say that I’ve got some files that I’ve tacked .del onto, so that I know that I don’t want them any more. For example:

Directory of F:\ToDo\archive

Wed 15 Nov 06 12:13 29 peek.bat.del

Wed 15 Nov 06 11:12 225 peek.pl.del

Wed 15 Nov 06 12:13 28 pop.bat.del

Wed 15 Nov 06 12:13 32 push.bat.del

4 File(s) 314 bytes

0 Dir(s) 3,808,210,944 bytes free

Now, let’s say that I’ve decided to archive them instead of deleting them, so I move them to another directory. I don’t want to keep the .del extension, but I don’t want to rename each file by hand (either in Windows Explorer, or from the command line).

I know that I can rename each file individually, e.g.,

rename peek.bat.del peek.bat

But simply making use of wildcards doesn’t work at all. For instance

rename *.bat.del *.bat

just leaves me with files with names like peek.bat.bat. Not what I’m looking for at all.

It’s possible to use the DOS “for” command to handle this, as long as you make use of tokens. For instance, I can’t just say something like

for %i in (*.*.del) do rename *.*.del *.*

It just doesn’t work.

After some web-searching, and general messing around, I can now do it with the following command (all on one line please):

for /f “tokens=1,2 delims=.” %i in (‘dir *.*.del /b’) do rename %i.%j.del %i.%j

And it’s really that easy! Here’s what the various bits of the command are:

  • for /f – We’re using the for command. The /f flag here means that we’re parsing the output of a command (the dir command that’s discussed below).
  • “tokens = 1,2 delims=.” – This breaks up each item into parts, each part deliminated by a period “.”. The 1,2 shows that I’m naming the first two tokens.
  • %i – What I’m going to call each element that I pull from the set. Actually, %i will be the first term, %j will be the second, etc.
  • in (‘dir *.*.del /b’) – This defines the set that I’m pulling each element out of. I got this fu from a Yahoo Answers page, and all it does is take a bare directory listing of my current directory, showing only those files that fit the *.*.del filename pattern.
  • do rename – I’m going to be performing the rename command on each element.
  • %i.%j.del – The rename command takes two arguments; the first is the name of the file to be renamed. Here, we see that each file to be renamed has three parts, the %i part (file name), the %j part (original extension of the file, e.g., bat, pl) and the .del part (final extension that the file now has).
  • %i.%j – The second argument is what I want the renamed file to be called. Here we’re just saying that we want the file to have its file name and original extension.
Example

Here’s what it would look like in action:

F:\ToDo\archive>for /f “tokens=1,2 delims=.” %i in (‘dir *.*.del /b’) do rename %i.%j.del %i.%j

F:\ToDo\archive>rename peek.bat.del peek.bat

F:\ToDo\archive>rename peek.pl.del peek.pl

F:\ToDo\archive>rename pop.bat.del pop.bat

F:\ToDo\archive>rename push.bat.del push.bat

Note: I typed the first line, DOS took care of the rest. And here’s what I see in my directory now:

Wed 15 Nov 06 12:13 29 peek.bat

Wed 15 Nov 06 11:12 225 peek.pl

Wed 15 Nov 06 12:13 28 pop.bat

Wed 15 Nov 06 12:13 32 push.bat

Sources
  • Impetus to try figuring out the FOR command, as I’ve never really gotten into it: LifeHacker post.
  • Help on FOR command: “help for” at command prompt
  • Help on setting up tokens and dir command to deal with file names: Yahoo answers page.

Categories: dos, scripts/batch

Source: GTD Wannabe: Using FOR to Rename Files from Command Line

 

  1. Posting comments is disallowed.