复制文件夹并将它们存储在文本文件中,以便在下次运行时排除曾经复制的文件夹 [英] Copy folders and store them in text file to exclude once copied folders on next run

查看:14
本文介绍了复制文件夹并将它们存储在文本文件中,以便在下次运行时排除曾经复制的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写一个脚本来监视文件夹 H:Start 并将其文件在 H:Start 中的新子文件夹复制到新位置 H:目标.该脚本应将复制的文件夹和文件存储在文本文件中.

I try to write a script that can monitor a folder H:Start and copy a new subfolder with its files in H:Start to new location H:Target. The script should store the copied folder and files in a text file.

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

Each time the script starts new and monitors H:Start, it should check the text file and copy only those subfolders which are not yet included in the text file because copied already before.

我在万维网上搜索示例,但找不到真正的起点.任何帮助都会很好.

I was searching the world wide web for examples, but could not really find a starting point. Any help would be nice.

到目前为止,我的工作并不好:)

I have so far not working good :)

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

推荐答案

我建议使用:

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

有关 xcopy 的所有这些参数的信息,请打开命令提示符窗口并在其中运行 xcopy/? 以获得此命令的帮助,该命令解释了所有这些参数.

For information about all those parameters of xcopy open a command prompt window and run there xcopy /? to get help of this command displayed which explains all those parameters.

重要的是/M,它只选择具有归档属性集的文件进行复制处理,并在复制后删除H:Start中每个文件的归档属性文件.这样可以避免一次复制的文件被再次复制一次,只要自上次复制以来没有在 H:Start 中修改.

The important one is /M which selects for copying process just files with archive attribute set and which removes the archive attribute on each file in H:Start after copying the file. This avoids that a once copied file is copied once more as long as not modified in H:Start since last copy.

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

If you want to log all copied files into a text file, I suggest using:

%SystemRoot%System32xcopy.exe H:Start H:Target /C /F /H /I /K /M /R /S /Y >>C:Desktoplist.txt

复制文件的名称是用这个命令行附加到文本文件C:Desktoplist.txt

The names of the copied files are with this command line appended to text file C:Desktoplist.txt

以下带注释的批处理代码可按要求与目录列表一起使用.

The following commented batch code works with directory lists as asked for.

@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:Desktoplist.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%System32findstr.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%System32findstr.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%System32xcopy.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 复制,只要删除的目录也不会从主列表文件中删除.

This batch file should work for the FTP folder mounted as drive on Windows. It does not depend on attributes or timestamps. It uses explicitly only the names of the directories in H:Start. It also does not check which directories exist already in H:Target. Therefore it is possible to delete a directory in H:Target if not interested in and the deleted directory will be nevertheless not copied once again from H:Start as long as the deleted directory is not also removed from main list file.

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

For details on parameters used on findstr run in a command prompt window findstr /? and read the entire help output into the window.

感谢这个问题,因为这确实是一个有趣的批处理编码任务.

Thanks for this question as this was really an interesting batch coding task.

这篇关于复制文件夹并将它们存储在文本文件中,以便在下次运行时排除曾经复制的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆