【问题标题】:Need batch program that moves files without copying then deleting them需要移动文件而不复制然后删除它们的批处理程序
【发布时间】:2015-04-01 19:11:55
【问题描述】:

每个月我在特定文件夹中都有大量文件(在许多子文件夹中)。我需要将它们全部移动到不同的文件夹中。为了使移动它们的过程自动化,我在批处理文件中使用了 robocopy。它工作正常,但需要 HOURS 才能运行。 (很多很多 GB)。

现在,如果我在 Windows 资源管理器中手动执行此操作,打开所述文件夹,选择所有文件夹,然后右键拖动到目标文件夹,然后选择“移至此处”,它会立即移动 。 (Windows XP 必须修剪和移植目录条目,而无需制作文件的第二份副本。...是的,源和目标位于同一分区上。)

所以,问题是:有谁知道我可以从批处理文件运行的程序以这种瞬时方式移动文件? (需要移动整个子文件夹树)

【问题讨论】:

  • moveren(重命名)。
  • 我不相信 DOS ren 命令会将整个目录树从源目录移动到目标目录。如果我错了,请纠正我...
  • 您是否在 ROBOCOPY 中使用了/MOVE 参数?
  • 是的,@aphoria,我是。

标签: windows batch-file file-moving


【解决方案1】:

您可以为此使用MOVE

C:\>MOVE /?
Moves files and renames files and directories.

To move one or more files:
MOVE [/Y | /-Y] [drive:][path]filename1[,...] destination

To rename a directory:
MOVE [/Y | /-Y] [drive:][path]dirname1 dirname2

      [drive:][path]filename1 Specifies the location and name of the file
                          or files you want to move.
  destination             Specifies the new location of the file. Destination
                          can consist of a drive letter and colon, a
                          directory name, or a combination. If you are moving
                          only one file, you can also include a filename if
                          you want to rename the file when you move it.
  [drive:][path]dirname1  Specifies the directory you want to rename.
  dirname2                Specifies the new name of the directory.

  /Y                      Suppresses prompting to confirm you want to
                          overwrite an existing destination file.
  /-Y                     Causes prompting to confirm you want to overwrite
                          an existing destination file.

The switch /Y may be present in the COPYCMD environment variable.
This may be overridden with /-Y on the command line.  Default is
to prompt on overwrites unless MOVE command is being executed from
within a batch script.

例如:

C:\Users\test>mkdir to
C:\Users\test>move from\*.txt to
C:\Users\test\from\1.txt
C:\Users\test\from\2.txt
        2 file(s) moved.

【讨论】:

  • 在一个 200 GB 的文件上尝试了 MOVE,你是对的,它是即时的(在同一个分区内)!如果我不尝试,我不会相信它。但是现在我需要找一些奇怪复杂的 FOR 循环来递归子目录结构??
  • 试图投票给你的答案,但没有代表。
  • @Tom:您不必递归 - 您可以在目录上使用 MOVE,它会一次性移动目录及其全部内容。
  • 它不会为您递归目录,也无法将整个目录树(包含子文件夹和文件)移动到分区上完全不同的文件夹内的目标位置。我试过了:move "L:\Backups\Source" L:\DEST 它说“拒绝访问” 我试过move "L:\Backups\Source\*.*" L:\DEST 它只移动了 Source 中的文件,而不是子文件夹或其文件。
  • @Tom:我保证,它可以一次性移动整个目录树!在你的情况下可能还有另一个问题阻止它,但我已经尝试了很多次并且它确实有效。您确定您运行的用户有权移动(并因此删除)Source 文件夹本身,而不仅仅是其内容?
【解决方案2】:

@psmears 指出了正确的方向,并进行了大量谷歌搜索,我找到了(或一个)解决方案:

@echo off

setlocal ENABLEDELAYEDEXPANSION


REM  ** Change to source dir

L:
cd "L:\Backups\Source"



REM  ** Move files recursively

for /R %%G in (.) do (

set mydir=%%G
set mynewdir=!mydir:~18!

md "L:\DEST\!mynewdir!"
cd "L:\DEST\!mynewdir!"
move "%%G\*.*" .

)



REM  ** Remove now-empty sub-dir structure inside source

L:
cd "L:\Backups\Source"

for /f "usebackq delims=" %%d in (`"dir /ad/b/s | sort /R"`) do rd "%%d"

【讨论】:

    猜你喜欢
    • 2023-04-05
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2015-08-09
    相关资源
    最近更新 更多