【问题标题】:How use a batch to copy two of the most recent of each type of file from a folder with many types of logs如何使用批处理从具有多种日志类型的文件夹中复制每种类型文件的两个最新文件
【发布时间】:2019-11-14 02:35:24
【问题描述】:

我发现一个帖子,用户想要将 2 个最近的日志文件夹复制到另一个位置 answered by Mofi。我正在尝试类似的事情,除了我有一个包含多种类型的故障排除日志的文件夹。我试图只复制特定的日志,但效果好坏参半。我能够为两种日志类型复制两个最新的日志,但在其他日志类型中没有成功。似乎在批处理中使用暂停并没有停止它以查看任何错误消息。

我成功复制了本地主机日志和服务器日志。但是,诊断日志不会复制或我尝试移动的文件夹中的任何其他日志。该文件夹中的所有文件都是 .log 扩展名,但在一定大小后转为 name.log.date 格式,但这对于 localhost 和服务器日志似乎无关紧要。我尝试在每个块的末尾甚至在块内设置暂停,但批处理不会停止,所以我没有机会看到任何错误。

@echo off
mkdir N:\Copy_logs
setlocal EnableExtensions EnableDelayedExpansion

REM -----------------
REM localhostlog
REM -----------------

set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"

set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"

if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"

for /F "eol=| delims=" %%I in ('dir "%SourcePath%/localhost_access_log.*" /A-D /B /O-D 2^>nul') do (
    %SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
    set /A FileCount-=1
    if !FileCount! == 0 goto serverLog
)
REM -----------------
REM serverLog
REM -----------------
:serverLog
set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"

set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"

if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"

for /F "eol=| delims=" %%I in ('dir "%SourcePath%/server.*" /A-D /B /O-D 2^>nul') do (
    %SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
    set /A FileCount-=1
    if !FileCount! == 0 goto diagLog
)
REM -----------------
REM Diagnostic Log
REM -----------------
:diagLog
set FileCount=02
set "SourcePath=D:\applications\server\log"
set "TargetPath=N:\Copy_logs"

set "SourcePath=%SourcePath:/=\%"
set "TargetPath=%TargetPath:/=\%"

if not "%SourcePath:~-1%" == "\" set "SourcePath=%SourcePath%\"
if not "%TargetPath:~-1%" == "\" set "TargetPath=%TargetPath%\"

for /F "eol=| delims=" %%I in ('dir "%SourcePath%/diagnostic.*" /A-D /B /O-D 2^>nul') do (
    %SystemRoot%\System32\xcopy.exe "%SourcePath%%%I" "%TargetPath%" /C /I /Q /H /R /Y >nul
    set /A FileCount-=1
    if !FileCount! == 0 goto :FileCopyDone
)

:FileCopyDone
endlocal

预期结果是复制每种日志类型的两个最新文件。前两种类型的日志会复制,但其余的任何日志都不会复制。

【问题讨论】:

    标签: windows batch-file cmd


    【解决方案1】:

    我建议你使用子函数。

    未经测试的快速示例:

    @Echo Off
    Set "source=D:\applications\server\log"
    Set "destination=N:\Copy_logs"
    Set "number=2"
    PushD "%source%" 2>NUL || GoTo :EOF
    For %%A In ("localhost_access_log" "server" "diagnostic") Do If Exist "%%~A.*" Call :Sub %%A
    PopD
    GoTo :EOF
    
    :Sub
    For /F "Tokens=1*Delims=[]" %%A In (
        'Dir /B/A-D/O-D/TW "%~1.*" 2^>NUL^|"%__AppDir__%find.exe" /N /V ""') Do (
        If %%A GTR %number% Exit /B
        "%__AppDir__%xcopy.exe" "%source%\%%B" "%destination%\" /Y)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多