Batch file 重新创建目录结构并复制最新文件

Batch file 重新创建目录结构并复制最新文件,batch-file,command-line,Batch File,Command Line,我有100多个子目录,都在同一个文件夹下,我希望将最新文件复制到备份位置,目录结构保持不变 \data\sub1\newest.file -> \backup\sub1\newest.file \data\sub1\older.file1.ignore \data\sub1\older.file2.ignore \data\sub2\newest.file -> \backup\sub2\newest.file \data\sub2\older.file1.ignore etc...

我有100多个子目录,都在同一个文件夹下,我希望将最新文件复制到备份位置,目录结构保持不变

\data\sub1\newest.file -> \backup\sub1\newest.file
\data\sub1\older.file1.ignore
\data\sub1\older.file2.ignore
\data\sub2\newest.file -> \backup\sub2\newest.file
\data\sub2\older.file1.ignore
etc....
这是我到目前为止所拥有的,我似乎无法把它拼凑起来。任何帮助都将不胜感激

@echo off

set source="c:\data"

set dest="n:\backup"

if not exist %dest% md %dest%

cd /d %source%

for /d  %%x in ("%source%"/*.*) do (

if not exist "%dest%\%%x" md "%dest%\%%x"

FOR /F %%I IN ('DIR *.* /A-D /B /O-D') DO COPY %%I "%DEST%\%%X" & @ECHO %%I     COPIED TO "%DEST%\%%X"

)

如果我是你,我会尝试这样做,因为这很可能是一个更健壮的解决方案。

Windows内置的
xcopy
程序提供了一个包含空目录的标志

C:\>xcopy "%source%" "%dest%" /E
但是,听起来您可能只想复制较新/丢失的文件。如果是这样的话,那么@Marged是对的。您应该使用
robocopy

C:\>robocopy "%source%" "%dest%" /E

查看
robocopy/?
以了解所有详细信息和其他命令。

for语句中的元变量
%%x
区分大小写,因此必须在整个循环中使用
%%x
,但在
copy
语句中使用的是
%%x

由于您只想复制第一个文件,因此应在复制第一个文件后附加
&转到alabelotsideoftheforloop
,以终止..x..的