问题描述
我有一个目录,其中包含文件和多个级别的子目录:
I have a directory which contains files and a number of levels of subdirectories:
C:Source
我想将 C:Source 的内容移动到:
I would like to move the contents of C:Source into:
C:Destination
要求:
- 所有文件和所有子目录内 C:SourceData 必须移动
- 我将在一个批处理文件
- 我无法使用 Powershell 或任何其他脚本语言
- All files and all subdirectorieswithin C:SourceData must be moved
- I will be running the command in abatch file
- I can't use Powershell orany other scripting languages
尝试 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.
尝试 1
MOVE "C:Source" "C:Destination"
这会将整个 C:Source 目录移动到 C:Destination 所以我最终得到:
This moves the entire C:Source directory into C:Destination so I end up with:
C:DestinationSource
尝试 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"
这会移动 C:Source 中的文件,但不会移动子目录或其内容.请注意,我在批处理文件中使用了 %%x 而不是 %x.
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.
使用 FOR 似乎很有前途,但我不确定我是否使用了正确的语法?我错过了什么吗?
Using FOR seems promising but I'm not sure I've used the right syntax? Have I missed something?
尝试 3
按照 Nick D 的建议,我尝试重命名:
As suggested by Nick D, I tried rename:
重命名C:Source"目的地
RENAME "C:Source" Destination
对于我给出的示例场景,这很好用.不幸的是,我真正的 Destination 目录与 Source 目录位于不同的级别,并且这似乎不受支持:
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"
推荐答案
更新:
将代码调整为a.检查目标文件夹是否已存在,在这种情况下,将文件夹中的文件移过(并继续遍历源目录结构),否则将整个文件夹移动.
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
这篇关于如何将一个目录树的内容移动到另一个目录树中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!