Home » Questions » Computers [ Ask a new question ]

Which command can I use to recursively rename or move a file in Windows?

Which command can I use to recursively rename or move a file in Windows?

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

Asked by: Guest | Views: 138
Total answers/comments: 5
Guest [Entry]

"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"
Guest [Entry]

"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


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."
Guest [Entry]

"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"
Guest [Entry]

"There are Windows ports for most UNIX commands:

GNU utilities for Win32"
Guest [Entry]

"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

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
Index = 0
Redim FileList(Files.Count-1)
For Each File In Files
set FileList(Index) = File
Index = Index + 1
GetFileArray = FileList
End If

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

End function"