views:

1032

answers:

8

I have a directory which contains files and a number of levels of subdirectories:

C:\Source

I would like to move the contents of C:\Source into:

C:\Destination

Requirements:

  • All files and all subdirectories within C:\SourceData must be moved
  • I will be running the command in a batch file
  • I can't use Powershell or any other scripting languages

Attempt 0

XCOPY /E "C:\Source" "C:\Destination"

This works perfectly, but it copies instead of moves. I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time.

Attempt 1

MOVE "C:\Source" "C:\Destination"

This moves the entire C:\Source directory into C:\Destination so I end up with:

C:\Destination\Source

Attempt 2

With some help from this question and accepted answer I came up with:

for /r "C:\Source" %%x in (*) do move "%%x" "C:\Destination"

This moves the files within C:\Source but not the subdirectories or their contents. Note that I used %%x instead of %x as I'm using it in a batch file.

Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?

Attempt 3

As suggested by Nick D, I tried rename:

RENAME "C:\Source" Destination

For the example scenario I gave this works fine. Unfortunately my real Destination directory is at a different level to the Source directory and this doesn't seem to be supported:

C:\>REN /?
Renames a file or files.

RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.

Note that you cannot specify a new drive or path for your destination file.

I get "The syntax of the command is incorrect." errors if I try to specify a more complex destination path, for example:

RENAME "C:\Source" "C:\MyOtherDirectory\Destination"
RENAME "C:\Source" "MyOtherDirectory\Destination"
A: 

Since XCOPY works, you could use XCOPY and DELETE, it's a workaround, but should work?

StuperUser
That would work for a lot of cases but unfortunately I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time. I've updated the question to clarify this, I should have mentioned it before.
tjrobinson
A: 

on Vista use

robocopy source destination /MIR

/MIR .. mirror a complete directory tree (also deletes files in the destination)

else

xcopy

Of course you have to delete the source afterwards :)

Dynamicbyte
That would work for a lot of cases but unfortunately I can't copy then delete the source as I'm moving a very large set of files and there isn't enough disk space to have two copies of them at a time. I've updated the question to clarify this, I should have mentioned it before.
tjrobinson
yes, it would. You would need robocopy /MOVE not /MIR
Joey
A: 

I have a directory which contains files and a number of levels of subdirectories:

C:\Source

I would like to move the contents of C:\Source into:

C:\Destination

Maybe I'm missing something, but can't you just rename the folder?

Nick D
Unfortunately that doesn't seem to work if the Destination directory isn't at the same level as the Source directory. I'll add more detail to the question rather than this comments box.
tjrobinson
+2  A: 

Update:

Adjusted code to a. check whether folders already exist at the destination, in which case move files in that folder over (and continue traversing the source directory structure), otherwise move the folder wholesale.

At the end of the script the source folder is removed altogether to eliminate these folders which have had their files moved over to an already existent folder at the destination (meaning these folders have been emptied but not deleted at the source).

Additionally we check whether a folder is both empty and already exists at the destination in which case we do nothing (and leave the source folder to be deleted to the last line of the script). Not doing this results in "The filename, directory name, or volume label syntax is incorrect." errors.

Phew! Please let me know how you get on with this! I have tested this and it seems to be working well.

for /d /r "c:\source" %%i in (*) do if exist "c:\destination\%%~ni" (dir "%%i" | find "0 File(s)" > NUL & if errorlevel 1 move /y "%%i\*.*" "c:\destination\%%~ni") else (move /y "%%i" "c:\destination")
move /y c:\source\*.* c:\destination
rd /s /q c:\source
kronoz
I needed to change the FOR command to use %%i instead of %i (as it's in a batch file) but other than that it works great. Thank you!
tjrobinson
Yay! That really was a guess to be honest. Noticed /D in the for help screen and took a shot... I updated the %%i thing :)
kronoz
Hmm, maybe it's not working after all. I'm getting "The filename, directory name, or volume label syntax is incorrect." when running the move command.
tjrobinson
OK, I've significantly updating the answer to try to address issues I found with it in the first place, hope it sorts it now!
kronoz
Thanks, I'll give this a try but have run out of time for now. I went with the robocopy option in the end as it's easier to read and understand for whoever inherits my code at a later date.
tjrobinson
+1  A: 

Undoubtedly use robocopy. It is a simple but brilliantly useful tool.

robocopy /move /e sourcedir destdir

This will move all the files and folders, including empty ones, deleting each original file after it has moved it.

If you don't have robocopy installed you can download it by itself or as part of a Microsoft resource kit.

DavidMWilliams
+1: I'd forgotten about robocopy being able to move (must be the name!). In my case I can't rely on robocopy being installed but I could easily include the binary alongside the script.
tjrobinson
Yeah - and what's nice is it doesn't have any dependencies; it is just one .exe and works fine on Vista, Server 2003, 2008, etc. So long as you can store it with the script you'll have a robust and reliable process.
DavidMWilliams
+1  A: 

As sent on twitter: Try combining attempt 3 with attempt 1. Rename to get the destination folder correct, then move "Destination" to the correct place.

David Webster
A: 
@echo on
set SOURCE=C:\Source
set DESTINATION=C:\Destination

xcopy %SOURCE%\* %DESTINATION%\* /s /e /i /Y

PAUSE

i use batch file like this...

or simply call:

xcopy C:\Source\* C:\Destination\* /s /e /i /Y
ufukgun
What do those /s /e /i /Y switches do? Do they make it move the files rather than copy them?
tjrobinson
A: 

I think the easiest thing you could do is to modify your Attempt 2

from

MOVE "C:\Source" "C:\Destination"

to

MOVE "C:\Source\*" "C:\Destination"

KISS ;-)

Edit: this seem not to work, so my advice is to trash away the crappy DOS command line and use Cygwin with BASH as a shell! (or just add the cygwin binaries to the path so you can use mv within DOS, thus not having to change the shell as your requirements state).

fortran
That's very simple but doesn't work. Subdirectories within C:\Source aren't copied
tjrobinson
aren't they? :-s what a crap...
fortran