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.

I want to rename multiple folders and keep all files and subdirectories in tact.
Windows batch commands like xcopy and move can't handle every case*.

I set up two columns with old paths and new paths.

OLD PATH            NEW PATH
C:\dir1\dir3\dir7   C:\a1\a2\a3      
C:\dir1\dir4\dir8   C:\b1\b2\b3      
C:\dir2\dir5\dir9   C:\c1\c2\c3      
...
C:\dir2\dir6\dir0   C:\d1\d2\d3      

Difficulties: More than one folder per line needs to be renamed; last listed folders like dir7 can have multiple subdirectories; file time stamps shouldn't be modified


* xcopy can't move, it only copies. That's a problem if you haven't enough space to copy and delete big data.
The move command doesn't move subdirectories, only files

share|improve this question
    
Was this a real question? Did you come up with your own answer within a minute? Or is it your personal "howto"? –  Rik Nov 20 '13 at 21:39
    
superuser.com/help/self-answer –  nixda Nov 20 '13 at 22:17
    
Okay. My bad. Didn't know that. –  Rik Nov 20 '13 at 22:22

1 Answer 1

up vote 1 down vote accepted

The most simple (straightforward) batchfile version would be like this:

call :robo "C:\dir1\dir3\dir7"   "C:\a1\a2\a3"
call :robo "C:\dir1\dir4\dir8"   "C:\b1\b2\b3"
call :robo "C:\dir2\dir5\dir9"   "C:\c1\c2\c3"
echo etc...
call :robo "C:\dir2\dir6\dir0"   "C:\d1\d2\d3"
goto:eof

:robo
robocopy %1 %2 /MOVE /E
goto:eof

You would need to quote the directories in case they have spaces.

For reading the directories out of a file you would do this:

directories.txt:

C:\dir1\dir3\dir7,C:\a1\a2\a3
C:\dir1\dir4\dir8,C:\b1\b2\b3
C:\dir2\dir5\dir9,C:\c1\c2\c3
C:\dir2\dir6\dir0,C:\d1\d2\d3

No need for quotes and separate them with a comma. Then in the batchfile:

FOR /F "tokens=1-2* delims=," %%A IN (directories.txt) DO (
  robocopy "%%A" "%%B" /MOVE /E
)

Also here... if you would like to see what robocopy does first use the /L.

If you have a file like this (with spaces to align the first and second column) you would need to add some code to strip the leading and trailing spaces from the directories:

Again the directories.txt:

C:\dir1\dir3\dir7\sada\asda ,  C:\a1\a2\a3
C:\dir1\dir4\dir8           ,  C:\b1\b2\b3
C:\dir2\dir5\dir9           ,  C:\c1\c2\c3
...
C:\dir2\dir6\dir0           , C:\d1\d2\d3

And this as batchfile:

@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1-2* delims=," %%A IN (directories.txt) DO (
  call :trim dir1 %%A
  call :trim dir2 %%B
  robocopy "!dir1!" "!dir2!" /MOVE /E
)
goto:eof

:trim
set trimmed=%2
for /f "tokens=* delims= " %%c in ("%trimmed%") do set trimmed=%%c
for /l %%c in (1,1,100) do if "!trimmed:~-1!"==" " set trimmed=!trimmed:~0,-1!
set "%~1=%trimmed%"
goto:eof

Note: No error checking is done to see if the directories really exists or if only one column is filled... etc...
(but of course that could be added).

share|improve this answer
    
I like the second approach, I will try this –  nixda Nov 21 '13 at 8:58

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.