复制文件夹并将其存储在文本文件中,以便在下次运行时排除复制的文件夹

时间:2015-07-27 03:49:52

标签: batch-file copy

我尝试编写一个可以监控文件夹H:\Start的脚本,并将包含H:\Start文件的新子文件夹复制到新位置H:\Target。该脚本应将复制的文件夹和文件存储在文本文件中。

每次脚本启动新的并监视H:\Start时,它应检查文本文件并仅复制那些尚未包含在文本文件中的子文件夹,因为之前已复制过。

我在万维网上搜索示例,但实际上找不到起点。任何帮助都会很好。

到目前为止我还没有好好工作:)

@echo off
setlocal EnableDelayedExpansion
pushd %1
for /D %%d in (“H:\Start\*.*”) do set n=!n!;%%d
if defined n echo %n:~1% > C:\Desktop\list.txt
popd
endlocal
for /f %%i in (C:\Desktop\list.txt) do not (
    xcopy /d /s H:\Start H:\Target > C:\Desktop\list.txt >nul 2>&1
)

2 个答案:

答案 0 :(得分:2)

我建议使用:

%SystemRoot%\System32\xcopy.exe H:\Start H:\Target /C /H /I /K /M /Q /R /S /Y >nul

有关 xcopy 的所有参数的信息,请打开命令提示符窗口并在那里运行xcopy /?以获取显示此命令的帮助,该命令将解释所有这些参数。

重要的一个是/M,它选择复制过程只是设置了归档属性的文件,并在复制文件后删除H:\Start中每个文件的归档属性。这样可以避免再次复制一次复制的文件,只要自上次复制后H:\Start中没有修改过。

如果您想将所有复制的文件记录到文本文件中,我建议使用:

%SystemRoot%\System32\xcopy.exe H:\Start H:\Target /C /F /H /I /K /M /R /S /Y >>C:\Desktop\list.txt

复制文件的名称使用此命令行附加到文本文件C:\Desktop\list.txt

以下注释的批处理代码适用于要求的目录列表。

@echo off
rem Define source and destination directory as well as
rem the names of the used list files each with full path.
setlocal EnableExtensions
set "Source=H:\Start"
set "Destination=H:\Target"
set "MainDirList=C:\Desktop\list.txt"
set "CurrentList=%Temp%\CurrentList.tmp"
set "ExcludeList=%Temp%\ExcludeList.tmp"
set "FinalDoList=%Temp%\FinalDoList.tmp"

rem Write the names of all subdirectories found in source
rem directory into a list file in directory for temporary files.
dir /AD /B "%Source%">"%CurrentList%"

rem Check if list file is not empty because of no subdirectories.
call :CheckEmpty "%CurrentList%"
if %FileIsEmpty% == 1 (
    echo No directories in %Source%
    goto EndBatch
)

rem Start copying the directories if there is no main list file
rem from a previous execution of this batch file or the main list
rem file was deleted intentionally to force copying all again.
if not exist "%MainDirList%" goto CopyDirectories

rem Start copying also if main list file is an empty file.
call :CheckEmpty "%MainDirList%"
if %FileIsEmpty% == 1 del "%MainDirList%" & goto CopyDirectories

rem Search in main list for lines matching completely lines in current
rem list with ignoring case and write the found lines into an exclude
rem list file as those directories were copied already before.
%SystemRoot%\System32\findstr.exe /I /L /X /G:"%CurrentList%" "%MainDirList%" >"%ExcludeList%"

rem Copy all directories if no line in current list is found in main list.
if errorlevel 1 goto CopyDirectories

rem Get all lines from current list not listed also in exclude list.
%SystemRoot%\System32\findstr.exe /B /I /L /V /G:"%ExcludeList%" "%CurrentList%" >"%FinalDoList%"

rem Replace the current list with the reduced final list.
move /Y "%FinalDoList%" "%CurrentList%"

rem Check if remaining current list is not empty because
rem all subdirectories copied already before.
call :CheckEmpty "%CurrentList%"
if %FileIsEmpty% == 1 (
    echo Copied already before all directories in %Source%
    goto EndBatch
)

:CopyDirectories
rem Copy each directory in remaining current list file.
for /F "usebackq delims=" %%D in ("%CurrentList%") do (
    echo Coping %Source%\%%D
    %SystemRoot%\System32\xcopy.exe "%Source%\%%D" "%Destination%\%%D" /C /H /I /K /Q /R /S /Y >nul
)

rem Append the list of copied directories to main list file.
type "%CurrentList%">>"%MainDirList%"
goto EndBatch

:CheckEmpty
rem This little subroutine just checks if size of a list file is 0.
if %~z1 == 0 ( set "FileIsEmpty=1" ) else ( set "FileIsEmpty=0" )
goto:EOF

:EndBatch
rem Delete all not further needed listed files and environment variables.
del "%ExcludeList%" 2>nul
del "%CurrentList%"
endlocal

此批处理文件应适用于在Windows上作为驱动器挂载的FTP文件夹。它不依赖于属性或时间戳。它仅显式使用H:\Start中目录的名称。它也不会检查H:\Target中已存在哪些目录。因此,如果不感兴趣,可以删除H:\Target中的目录,只要删除的目录不从主列表文件中删除,删除的目录将不会再次从H:\Start复制。

有关 findstr 上使用的参数的详细信息,请在命令提示符窗口findstr /?中运行,并将整个帮助输出读入窗口。

感谢您提出这个问题,因为这真是一个有趣的批量编码任务。

答案 1 :(得分:1)

您无需存储任何内容,可以使用

xcopy /d /s h:\start h:\target
  

/ d:MM-DD-YYYY
           复制在指定日期或之后更改的文件。            如果没有给出日期,则仅复制其文件            来源日期/时间比目的地时间更新。

但是如果你需要一个文件列表,你可以使用重定向:

xcopy /d /s h:\start h:\target > logfile.txt