Fog Creek Software
Discussion Board

Rename in DOS

Can I remove the 1st character of a hundred of files
using the DOS command RENAME

If yes how ?

All my file have got the same structure


I want to remove the O (for Old) for all the files using
a dos command

Friday, June 20, 2003

I beleive you can use wildcards with rename


ren O* *

(try it on a sample diretory to make sure).

Friday, June 20, 2003

I think that wildcards will only work if you want to replace 1 or more characters with 1 or more different characters.  I couldn't figure out how to replace a character with "nothing".

I had to do the exact same thing the other day, so I just ended up using VB/VBA:

Public Function fDropFilePrefix( _
ByVal sPath As String, _
ByVal sPrefix As String _
) As Boolean

Dim sFileName As String
Dim sTemp As String

sFileName = Dir(sPath, vbNormal)
Do Until sFileName = ""
    sTemp = Mid(sFileName, Len(sPrefix) + 1)
    Name (sPath & sFileName) As (sPath & sTemp)
    sFileName = Dir

fDropFilePrefix = True

End Function

?fDropFilePrefix("C:\dev\old files\","old")

Tim Lara
Friday, June 20, 2003

oops...disregard the boolean return value.  I didn't bother to finish the error handling. ;-)

Tim Lara
Friday, June 20, 2003

You're probably right.  I'm pretty sure that

ren *.htm *.html

will work

Friday, June 20, 2003


If your files actually do begin with the letter O, then you can use the following DOS command to rename them:

for /f "delims=O usebackq" %i in (`dir /b O*`) @ren O%i %i

Norbert Burger
Friday, June 20, 2003

Yep, that does work.  Or:

ren old*.* new*.*

seems to work as expected, too, but I don't know an easy way to just get rid of the "old" part....

Tim Lara
Friday, June 20, 2003

Wow, Norbert.  That is way beyond my DOS capabilities.  Now I'm intrigued.

However, I couldn't get it to work on my machine - I get the message:

"@ren was unexpected at this time"

Is there a typo?

Tim Lara
Friday, June 20, 2003

Norbert's "for" command was, I'm almost sure, going to require that you're using cmd.exe from an NT-class OS.

Brad Wilson (
Friday, June 20, 2003

Norbert! You have just gained über geek status for that one.

Friday, June 20, 2003

I had some problems with "complex" renaming of files as well in windows, so I made a simple (very simple) utility that rename files using regex.

Saturday, June 21, 2003

Not targeting anyone here, but this talk of DOS and command prompts reminds me of one of my utmost pet hates -- people referring to the command line in NT/Win2K/XP as "a DOS window". AARRGH! It's not DOS when you see a command line (unless you type it's a true 32-bit environment, it's just not using windows, menus, pointers, etc. This was compounded by the fact that in NT4 the bloody marketing people got them to use the MS-DOS icon for the Command Prompt shortcut on the Start Menu!

Duncan Smart
Saturday, June 21, 2003

Norbert - that is some seriously hard core command line work.

I too can't stand it when people call the command prompt in Windows "DOS". DOS is a totally different operating system - the command prompt is just another way of interacting with Windows.

Daniel Searson
Saturday, June 21, 2003

An approach I used to use for one-off jobs.
1) dir o*.* > 1.bat
2) use a wordprocessor to replace "o" at the start of the line with "call 2.bat " at the start of each line. I used to use Wordperfect, but Word can do the same.
3) bit of manual fix-up, clear away the headings and count of files.
4) save 1.bat and then create 2.bat with a single line "ren o%1.%2 %1.%2"

It works for filenames are in 8.3 form.

Saturday, June 21, 2003

In case Norbert has taken the week-end off, the reason why the command doesn't execute "as-is" is that he forgot to type a "do" after the parenthesis.
"for /?" is your friend...

Roland Kaufmann
Saturday, June 21, 2003


Thanks; you're right -- I forgot the 'do'.  Here's the correct command:

for /f "delims=O usebackq" %i in (`dir /b O*`) do @ren O%i %i

Duncan, Daniel:

I agree completely, regarding the misconception that DOS is included in NT-class OSes.

Norbert Burger
Saturday, June 21, 2003

With the prices of hard drives as low as they are, why not just buy a new one?

Guy Incognito
Saturday, June 21, 2003

DOS is included in NT class  OSes, you can get it by running "command" instead of "cmd".

Joel Spolsky
Saturday, June 21, 2003

It's probably more appropriate to call it a DOS emulator rather than real DOS. NT-class OSes have shipped with, at various times, many emulators: DOS, Windows 3.x (WoW, or Windows on Windows), Posix, even one for OS/2 1.x command line applications.

Brad Wilson (
Saturday, June 21, 2003

For example the NT doesn't support "format /sys".

Christopher Wells
Saturday, June 21, 2003

The command window is not a DOS emulator, its upwardly compatible with the Command shell though entirely differently organised.

Simon Lucy
Sunday, June 22, 2003

A.T., try dir /b, so you won't have to get rid of the headers.

Monday, June 23, 2003

*  Recent Topics

*  Fog Creek Home