我如何可以移动一个目录树的内容到另一个? [英] How can I move the contents of one directory tree into another?

查看:213
本文介绍了我如何可以移动一个目录树的内容到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文件的目录,以及一些子目录层次:

  C:\\来源

我想移动C的内容:\\来源为:

  C:\\目的地

要求:


  • 所有文件和所有子目录
    的C:\\ SourceData必须移动

  • 我将运行一个命令
    批处理文件

  • 我不能使用PowerShell的或
    任何其他脚本语言

尝试0

  XCOPY / EC:\\源C:\\目的地

这完美的作品,但它拷贝而不是移动。我不能复制,然后删除源因为我动一个非常大的文件集,并没有足够的磁盘空间来让他们的两个副本在同一时间。

尝试1

  MOVEC:\\源C:\\目的地

这将整个C:\\ Source目录的的C:\\目的地,所以我结束了:

  C:\\目标\\来源

尝试二

这个问题的一些帮助和接受的答案我想出了:

 为/ RC:\\源%%倍In(*)不动%% XC:\\目的地

这移动在C中的文件:\\来源,但不是子目录及其内容。请注意,我用的x %%代替%×如我在批处理文件中使用它。

使用FOR似乎很有前途,但我不知道我用正确的语法?我错过了什么?

尝试3

正如尼克Ð建议,我尝试重命名:

重命名C:\\源目标

对于示例场景我给这工作正常。不幸的是我真正的目标的目录是在不同的级别到的来源的目录,这似乎并没有被支持:

  C:\\>任/?
重命名文件或文件。RENAME [盘符:] [路径]文件名1文件名2。
REN [盘符:] [路径]文件名1文件名2。请注意,您不能指定为目标文件新的驱动器或路径。

我得到命令的语法不正确。错误,如果我尝试指定一个更复杂的目标路径,例如:

 重命名C:\\源C:\\ MyOtherDirectory \\目的地
重命名C:\\源MyOtherDirectory \\目的地


解决方案

更新

调整code到。检查文件夹是否已存在的目的地,在这种情况下,移动该文件夹中多文件(并继续遍历源目录结构),否则移动文件夹批发。

在脚本的末尾源文件夹被完全删除,以消除这些文件夹其中有他们的文件转移到了一个已经存在的文件夹在目的地(即这些文件夹都被清空,但不是在源中删除)。

此外,我们检查文件夹是否为空以及已经在这种情况下,我们什么都不做的目标存在(并保留源文件夹中删除到脚本的最后一行)。不这样做会导致文件名,目录名或卷标语法不正确。错误。

唷!请让我知道你是怎么得到这个!我测试了这一点,它似乎运作良好。

 为/ D / RC:\\源%% i的(*)做是否存在C:\\目标\\ %%〜妮(DIR我%% |查找0文件(S)> NUL&放大器;如果错误级别1移动/ Y%% I \\ *。*C:\\目标\\ %%〜妮)否则(移动/ Y%%。我C:\\目标)
移动/ Y C:\\源\\ *。* C:\\目的地
RD / S / Q C:\\源

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"

解决方案

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

这篇关于我如何可以移动一个目录树的内容到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆