Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

windows - How can I move the contents of one directory tree into another?

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:DestinationSource

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:MyOtherDirectoryDestination"
RENAME "C:Source" "MyOtherDirectoryDestination"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...