Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What command in Windows emulates the recursive move / rename command from Unix?

share|improve this question

migrated from stackoverflow.com Oct 30 '10 at 5:12

This question came from our site for professional and enthusiast programmers.

12 Answers 12

up vote 46 down vote accepted

Use XP's for command. For example from the command line (in a batch file use %%x instead) to do a recursive move do:

for /r %x in (foo) do move "%x" "drive:\path\bar"

To do a recursive rename do:

for /r %x in (*.c) do ren "%x" *.cpp

Example batch:

for /r "< DIR >" %%x in (*.c) do ren "%%x" *.cpp
share|improve this answer
    
This is by far the simplest solution, thanks much! –  javamonkey79 Nov 6 '08 at 7:06
    
To use the FOR command in a batch program, specify %% variable instead of % variable. –  tjrobinson Jul 13 '09 at 9:44
    
How do you do this to a different source directory without cwd'ing? –  Evan Carroll Jun 19 '13 at 20:20

robocopy "C:\Source Folder" "C:\Destination Folder" /E /COPYALL /XJ

share|improve this answer
    
Can you give a little more details about this? I know this may seem trivial, but it would be useful to those that don't know what's going on here to know what the parameters are doing exactly. –  KronoS Oct 31 '12 at 17:59

I just run a small example in my Windows XP SP2 box with the move command and it worked. All files and directories were moved from source to dest. source and dest are directory names.

move source dest
ver

Microsoft Windows XP [Version 5.1.2600]
move /?

Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

  [drive:][path]filename1 Specifies the location and name of the file
                          or files you want to move.
  destination             Specifies the new location of the file. Destination
                          can consist of a drive letter and colon, a
                          directory name, or a combination. If you are moving
                          only one file, you can also include a filename if
                          you want to rename the file when you move it.
  [drive:][path]dirname1  Specifies the directory you want to rename.
  dirname2                Specifies the new name of the directory.

  /Y                      Suppresses prompting to confirm you want to
                          overwrite an existing destination file.
  /-Y                     Causes prompting to confirm you want to overwrite
                          an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.
share|improve this answer
    
I am really looking for recursive rename, sorry I included the move as part of the question. –  javamonkey79 Oct 16 '08 at 22:03

The built-in XCOPY command is close. It will do a recursive copy, but I don't think it supports rename.

share|improve this answer
1  
don't forget that xcopy will ignore hidden files by default. –  Richard Harrison Oct 16 '08 at 22:14

I've created a VB Script that will do a search and replace on directory names... I have a files version too, however, I think this is enough to get you started with your own script. The way i use this script is i have a fileandreplacedirs.vbs, and put it in the same folder as the folders I want to rename. Also, it doesn't necessarily recurse into the folder, but could with a little modification

search1  = InputBox("Search for...", "", "")
replace1 = InputBox("replace with...", "", "")

Dim MyFile
MyFiles = GetFileArray(".")

For Each MyFile In MyFiles
    NewFilename = Replace(MyFile.Name, search1, replace1)
    If InStr( MyFile.Name, search1 ) Then MyFile.Name = NewFilename
Next

MsgBox "Done..."

function GetFileArray(ByVal vPath)
    'Get our objects...
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.Getfolder(vPath)
    Set Files = Folder.SubFolders

    'Resize the local array
    'Arrays are 0 based but Files collection is 1 based.
    if Files.count = 0 then
    	GetFileArray = array()
    	Exit Function
    Else
    	Index = 0
    	Redim FileList(Files.Count-1)
    	For Each File In Files
    		set FileList(Index) = File
    		Index = Index + 1
    	Next
    	GetFileArray = FileList
    End If

    'Always good practice to explicitly release objects...
    Set FSO = Nothing
    Set Folder = Nothing
    Set Files = Nothing

End function
share|improve this answer
for /r %%x in (%1) do ren "%%x" %2

this will rename file recursively :-)

save in a file give 2 arguments from extension and to extension.

ex: file name is test.bat command : test *.avi *.bmp

it renames all files with extension avi to bmp (in all subfolders :))

Note: This is correction for the post answered Oct 26 at 13:20 by Rob Kam. He gave for

/r %x in (*.c) do ren "%x" *.cpp

where as it shud have %% instead of %

share|improve this answer
    
The %% is needed only for batch files. –  paradroid Oct 30 '10 at 6:56

This worked better for me:

FOR /R "C:\folder1\folder2\" %i in (.,*) DO MOVE /Y "%i" "C:\folder1\"

Source: http://www.islamadel.com/index.php/notes/6-computer/10-windows-command-line

share|improve this answer

There are Windows ports for most UNIX commands:

share|improve this answer

I found this python script that works as well:


         for root, dirs, files in os.walk(cur_dir):  
             for filename in files:  
                 file_ext = os.path.splitext(filename)[1]  
                 if old_ext == file_ext:  
                     oldname = os.path.join(root, filename)  
                     newname = oldname.replace(old_ext, new_ext)  
                     os.rename(oldname, newname)

Found at: http://gomputor.wordpress.com/2008/09/29/change-the-extension-of-multiple-files-in-a-chosen-directory-with-python/

I added Python to my path and put the Python script above in a 'utils' folder. I then created this simple batch script to run it: rn.bat:


python \utils\rn.py %1 %2 %3
I also updated the python script above to take its args from the command line.
rn.py:

import sys
import os

cur_dir = sys.argv[1]
old_ext = sys.argv[2]
new_ext = sys.argv[3]

#print cur_dir, old_ext, new_ext
for root, dirs, files in os.walk(cur_dir):  
    for filename in files:  
    	file_ext = os.path.splitext(filename)[1]

    	if old_ext == file_ext:  
    		oldname = os.path.join(root, filename)  
    		newname = oldname.replace(old_ext, new_ext)  
    		os.rename(oldname, newname)

Finally all one needs to do now is something like this:


>rn . .foo .bar

or


>rn \ .exe .txt

Have fun with the second one :)

share|improve this answer

Use a simple DOS command.

  1. cd to the source directory where you want to rename the file extensions recursively.

  2. Type this command:

    ren *.[CurrentFileExtension] *.[DesiredFileExtension]
    
share|improve this answer
1  
That works fine for the cwd, but not the subdirectories, thus it is not recursive. Thank you though :) –  javamonkey79 Oct 17 '08 at 17:06

I added if exist to avoid error returns (it matters in Jenkins so it doesn't breaks a build) when the file doesn't exists in every folder:

for /r %x in (foo) do if exist "%x" move "%x" "drive:\path\bar"

To do a recursive rename do:

for /r %x in (*.c) do if exist "%x" ren "%x" *.cpp

Example batch:

for /r "< DIR >" %%x in (*.c) do if exist "%%x" ren "%%x" *.cpp
share|improve this answer

Powershell is your friend. No batch files or scripts needed. For example, to recursivly rename all the .tiff files to .tif in a remote directory you can run the following command from Powershell:

get-childitem "\\\servername\d$\path" -recurse -Include *.tiff | Rename-Item -NewName { $_.Name.replace(".tiff",".tif") }

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.