Rename directory from command line

by admin on January 2, 2009

In Renaming files from command line we saw how to rename files using Rename command.   We can use Rename command to rename directories too. See the syntax below.

Rename  dir_oldname  dir_newname

Example:

G:\data>mkdir dir1
G:\data>dir /b
dir1
G:\data>rename dir1 dir2
G:\data>dir /b
dir2
G:\data>

Note that with rename command we can’t move the files to another drive or directory. This command just renames the existing file/folder and does not create a new file.  That is why it does not accept full path for the target file name. If you try to do that you will get the error – “The syntax of the command is incorrect.” Example:

C:\docs>ren 1.txt c:\docs\2.txt
The syntax of the command is incorrect.

The below rename command works fine.

C:\docs>ren c:\docs\1.txt 2.txt
C:\docs>dir /b
2.txt

We cal also use ren (the short form of rename) for rename operations. This command can be used to rename files or directories on all Windows OS editions i.e Windows 2000, XP, Vista and Windows 7.

{ 5 comments… read them below or add one }

Thai January 19, 2011 at 3:41 am

Can we use wildcats when renaming directories

Reply

Sumeeth Tewar February 4, 2011 at 6:29 am

rename *.txt *.doc
will definetely work :)
it does accept wild cards.

Reply

Techblogger February 4, 2011 at 5:13 pm

Thanks for correcting me Sumith. Removed my earlier comment.

Reply

Anonymous May 25, 2011 at 11:38 am

how do you deal with spaces in the directory you need to rename?

Reply

Techblogger May 25, 2011 at 11:43 am

Specify the directory name in quotes. See below examples

ren "directory with spaces" newdir

ren "directory with spaces" "another directory with spaces"

Reply

Leave a Comment

Previous post:

Next post: