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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…