需要批处理程序移动文件而不复制然后删除它们

时间:2015-04-01 19:11:55

标签: windows batch-file file-moving

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

现在,如果我在Windows资源管理器中手动执行此操作,打开所述文件夹,选择全部,右键拖动到目标文件夹,然后选择" Move Here",它会立即移动 。 (Windows XP必须修剪和嫁接目录条目,而不必复制文件。...是的,源和目标位于同一分区。)

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

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

从@psmears开始正确的方向,以及大量的Google搜索,我找到了(或者)解决方案:

@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"