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

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

问题描述

我尝试写一个脚本,可以监控一个文件夹 H:\\开始并复制其文件的新的子文件夹中的 H:\\启动新位置 H:\\目标。这个脚本应该存储在一个文本文件中复制的文件夹和文件。

每次脚本启动新的显示器 H:\\开始,它应该检查文本文件,并只复制中尚未包含在文本文件中,因为这些子文件夹之前已经被复制。

我正在寻找的例子万维网,但不能真正找到一个起点。任何帮助将是很好。

我迄今没有工作好:)

 关闭@echo
SETLOCAL EnableDelayedExpansion
PUSHD 1%
针对/ D %% d在(H:\\启动\\ *。*)!不要设定N = N; %%ð
如果n定义呼应%N:1〜%GT; C:\\桌面\\ LIST.TXT
POPD
ENDLOCAL
FOR / F %% i的(C:\\桌面\\ LIST.TXT)不(
    XCOPY / D / S H:\\启动H:\\&目标GT; C:\\桌面\\ LIST.TXT> NUL 2 - ;&放大器; 1


解决方案

我建议使用:

 的%SystemRoot%\\ SYSTEM32 \\ XCOPY.EXE H:\\启动H:\\目标/ C / H / I / K / M / Q / R / S / Y> NUL

有关 XCOPY 打开命令提示符窗口,然后运行有所有这些参数的信息XCOPY /?来得到这个命令的帮助显示哪些解释了所有的参数。

重要的是 / M 其中选择复制过程只是存档属性集的文件并消除了对每一个文件存档属性在 ^ h :\\启动复制后的文件。这避免了一次复制的文件被复制一次,只要没有修改 H:\\启动自去年副本

如果您希望将所有复制的文件记录到一个文本文件,我建议使用:

 的%SystemRoot%\\ SYSTEM32 \\ XCOPY.EXE H:\\启动H:\\目标/ C / F / H / I / K / M / R / S / Y>&GT ; C:\\桌面\\ LIST.TXT

复制的文件的名称与追加到文本文件此命令行 C:\\桌面\\ LIST.TXT


下面的评论批code。与目录列表作为工作要求。

 关闭@echo
REM定义源和目标目录,以及
REM每个完整路径使用的列表文件的名称。
SETLOCAL ENABLEEXTENSIONS
设置源= H:\\启动
一套目标= H:\\目标
设置MainDirList = C:\\桌面\\ LIST.TXT
设置CurrentList =%TEMP%\\ CurrentList.tmp
设置ExcludeList =%TEMP%\\ ExcludeList.tmp
设置FinalDoList =%TEMP%\\ FinalDoList.tmpREM写的所有子目录的源中找到的名字
REM目录到目录列表文件的临时文件。
DIR / AD / B%资料来源%>中%CurrentList%REM检查列表文件是因为没有子目录不是空的。
拨打:CheckEmpty%CurrentList%
如果%FileIsEmpty%== 1(
    呼应%资料来源%无目录
    endBatch函数转到
)REM开始复制的目录,如果没有主列表文件
这批文件的previous执行或主列表REM
REM文件已被删除故意所有强制重新复制。
如果不存在%MainDirList%转到CopyDirectoriesREM开始复制也主如果列表文件是一个空文件。
拨打:CheckEmpty%MainDirList%
如果%FileIsEmpty%== 1 DEL%MainDirList%&放大器;转到CopyDirectories在主力名单物权搜索匹配当前完全行线
与忽略大小写和写入找到的行到一个排除列表REM
REM列表文件因为这些目录被之前就已经被复制。
%SYSTEMROOT%\\ SYSTEM32 \\的Findstr.exe / I / L / X / G:%CurrentList%%MainDirList%>中%ExcludeList%REM复制所有的目录,如果当前列表中没有行主列表中找到。
如果ERRORLEVEL 1页转到CopyDirectoriesREM获得来自未列出的在排除列表中当前列表中的所有行。
%SYSTEMROOT%\\ SYSTEM32 \\的Findstr.exe / B / I / L / V / G:%ExcludeList%%CurrentList%>中%FinalDoList%REM替换当前列表的降低最终名单。
移动/ Y%FinalDoList%%CurrentList%REM检查剩余的当前目录是不是空的,因为
REM之前已经复制所有子目录。
拨打:CheckEmpty%CurrentList%
如果%FileIsEmpty%== 1(
    回声已经复制在%资料来源%的所有目录之前
    endBatch函数转到
):CopyDirectories
REM复制在剩下当前列表文件中的每个目录。
FOR / F有usebackq delims =%% D IN(%CurrentList%)做(
    回声应对%资料来源%\\ %%Ð
    %SYSTEMROOT%\\ SYSTEM32 \\ XCOPY.EXE%资料来源%\\ %% D%目标%\\ %% D/ C / H / I / K / Q / R / S / Y> NUL
)REM追加拷贝目录到主列表文件的列表。
键入%CurrentList%>>中%MainDirList%
endBatch函数转到:CheckEmpty
REM这个小子程序只是检查,如果一个列表文件的大小为0。
如果%〜Z1 == 0(设定FileIsEmpty = 1)其他(设定为FileIsEmpty = 0)
GOTO:EOF:endBatch函数
REM删除所有没有进一步需要列出的文件和环境变量。
德尔%ExcludeList%2 - ; NUL
德尔%CurrentList%
ENDLOCAL

此批处理文件应适用于安装Windows上的驱动器中的FTP文件夹。它不依赖于属性或时间戳。它采用明确只有目录的名称在 H:\\开始。它也不会检查这在已存在的目录H:\\目标。因此,可以删除 h的目录:\\目标如果不感兴趣,被删除的目录将仍然不会再次从复制H: \\开始只要删除的目录是不是也从主力名单文件中删除。

有关使用参数的详细 FINDSTR 在命令提示符窗口中运行 FINDSTR /?和读取整个帮助输出到窗口。

谢谢你这个问题,因为这确实是一个有趣的批号打码任务。

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.

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:\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
)

解决方案

I suggest using:

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

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.

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%\System32\xcopy.exe H:\Start H:\Target /C /F /H /I /K /M /R /S /Y >>C:\Desktop\list.txt

The names of the copied files are with this command line appended to text file C:\Desktop\list.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:\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

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.

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天全站免登陆