How use a batch to copy two of the most recent of

2019-08-19 02:50发布

I found a post where the user wanted to copy the 2 most recent log folders to another location answered by Mofi. I am attempting something similar except I have a folder that includes multiple types of troubleshooting logs. I have attempted to copy only specific logs with mixed success. I am able to copy two of the most recent logs for two of the log types but am having no success with the others. It also seems that using a pause in the batch is not stopping it to see any error messages.

I was successful in copying the localhost log and the server log. However, the diagnostic log does not copy or any other logs in the folder I attempt to move. All files in the folder are .log extensions but and after a certain size roll over to a name.log.date format, but that hasn't seemed to matter for the localhost and server logs. I have tried putting in pauses at the end of each block and even within the blocks but the batch doesn't stop so I do not get a chance to see any errors.

@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

The expected result is the copying of two of the most recent files of each type of log. The first two types of logs copy but any of the remaining logs do not copy.

1条回答
狗以群分
2楼-- · 2019-08-19 03:16

I would suggest that you use a sub function.

Quick untested example:

@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)
查看更多
登录 后发表回答