脚本批处理的日志文件

时间:2020-10-15 22:15:44

标签: windows cmd

@echo off
    call :checkFTP1 %* > all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :checkFTP2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :checkFTP3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :doCommands1 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :doCommands2 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :doCommands3 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    call :doCommands4 %* >> all_log_all_log_%date:~10,4%%date:~4,2%%date:~7,2%.log 2>&1
    exit /b
    
    
:checkFTP1

    @echo off
Setlocal

:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder1"') do set _TMP="%%a"
IF {%_TMP%}=={} (
    goto :Exit1
) ELSE (
    goto :checkFTP2
)
Endlocal


:checkFTP2
    @echo off
Setlocal

:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder2"') do set _TMP="%%a"
IF {%_TMP%}=={} (
    goto :Exit2
) ELSE (
    goto :checkFTP3
)
Endlocal


:checkFTP3
    @echo off
Setlocal

:: Is folder empty
set _TMP=
for /f "delims=" %%a in ('dir /b "C:\test\folder3"') do set _TMP="%%a"
IF {%_TMP%}=={} (
    goto :Exit3
) ELSE (
    goto :doCommands1
)
Endlocal


:doCommands1
    call script1.bat
    if %errorlevel% EQU 0 (goto :doCommands2 ) Else ( ECHO error on script 1 ,2,3,4)
    exit
:doCommands2
    call script2.bat
    if %errorlevel% EQU 0 (goto :doCommands3 ) Else ( ECHO Script 1 Completed  Successfully , ERRORS on  2,3,4)
    exit
:doCommands3
    call script3.bat
    if %errorlevel% EQU 0 (goto :doCommands4) Else ( ECHO Script 2 Completed  Successfully , ERRORS on 3,4)
    exit
:doCommands4
    call script4.bat
    if %errorlevel% EQU 0 (goto :completed1) Else ( ECHO Script 3 Completed  Successfully , ERRORS on  4)
    exit
:Exit1 

Echo Today Date  %DATE%  at %Time%
Echo ###################FTP-1 FILES  MISSING  #########################

Exit

:Exit2

Echo Today Date  %DATE%  at %Time%
Echo ###################FTP-2 FILES  MISSING (#########################

Exit

:Exit3

Echo Today Date  %DATE%  at %Time%
Echo ###################FTP-3 FILES  MISSING   #########################

Exit
    
    
:completed1


Echo Today Date  %DATE%  at %Time%
Echo ###################all scripts  Completed  Successfully#########################

Exit

我上面有一个批处理文件,它调用了多个蝙蝠文件。我已经测试了脚本,并且效果很好。
我唯一的问题是生成的日志文件包含所有信息,而且它是一个大文件。
是否可以只记录注释和回显,并排除屏幕中执行的内容?
例如,我不希望将1个文件移动到日志文件中。

1 个答案:

答案 0 :(得分:0)

我已将您的代码重新编写为使用通用功能,该通用功能将代码重复数据删除为更易于管理的形式,并允许您通过编辑以下内容的列表根据需要向FTP和Script部分添加或删除任何步骤:变量。

我还只返回不包含“已移动文件”的输出。

或者,如果您确实只希望打印状态行,则可以将其更改为仅将状态行打印到日志上,而不将所有信息打印到日志上(这样做是因为您将重定向重定向到日志上调用其他步骤。

这也是在实际执行所有goto命令之前不需要编写的方式。

这是重构的代码,我没有测试过,但是没关系,我必须走几个小时,您可以提出任何问题,有时间我会很乐意回答。

>
@( Setlocal EnableDelayedExpansion 
  echo off
  SET "_FolderList="C:\test\folder1" "C:\test\folder1" "C:\test\folder1" "
  SET "_ScriptList="c:\test\script1.bat" "E:\script2.bat" "C:\Path\To\script3.bat" "C:\Path\To\script4.bat" "
  SET "_Scripts_Failed=1,2,3,4"
  SET "_Scripts_Completed="
  SET "_Continue=0"
  CALL :GetDateTime
  SET "MasterLog=all_log_all_log_!IsoDate!_!IsoTime!.log"
)

CALL :Main

( Endlocal
  EXIT /B
)

:Main
  SET /A "_Counter=0"
  FOR %%_ IN (%_FolderList%) DO (
    IF DEFINED _Continue (
      SET /A "_Counter+=1"
      CALL :CheckFTP_List %%~_
    )
  )>> "%MasterLog%"
  SET /A "_Counter=0"
  FOR %%_ IN (%_FolderList%) DO (
    IF DEFINED _Continue (
      SET /A "_Counter+=1"
      CALL :DoCommands_List %%~_
    )
  )>> "%MasterLog%"
  IF DEFINED _Continue (
    Echo Today Date  %DATE%  at %Time%
    Echo ###################all scripts  Completed Successfully#########################
  )
GOTO :EOF
  
  
:CheckFTP_List
  REM Is folder empty
  for /f "delims=" %%a in ('dir /b /A-D "%*') do (
    set "_Continue=%%a" )
  IF NOT Defined _Continue (
    Echo.Today Date on %DATE%  at %Time%
    Echo.###################FTP-%_Counter% FILES  MISSING  #########################
  )
GOTO :EOF

:DoCommands_List
  call "*%" | FIND /I /V "file(s) moved" &REM only outputs lines that don't contain files moved.
  if %errorlevel% NEQ 0 (
    SET "_Continue="
    SET "_Scripts_Completed=%_Scripts_Completed:,1=1%"
    SET "_Scripts_Failed=!_Scripts_Failed:%_Scripts_Completed%=!"
    Echo Today Date  %DATE%  at %Time%
    ECHO. Error encountered on Script %Counter%! -- Completed Scripts: !_Scripts_Completed! -- Failed Scripts: !_Scripts_Failed!
  ) ELSE (
    SET "_Scripts_Completed=%_Scripts_Completed:,1=1%,%Counter%"
  )
GOTO :EOF



:GetDateTime
  FOR /F "Tokens=1-7 delims=MTWFSmtwfsouehrandit:-\/. " %%A IN ("%DATE% %TIME: =0%") DO (
    FOR /F "Tokens=2-4 Delims=(-)" %%a IN ('ECHO.^| DATE') DO (
      SET "%%~a=%%~A"
      SET "%%~b=%%~B"
      SET "%%~c=%%~C"
      SET "HH=%%~D"
      SET "Mn=%%~E"
      SET "SS=%%~F"
      SET "Ms=%%~G"
    )
  )
  SET "IsoTime=%HH%.%Mn%.%SS%.%Ms%"
  SET "IsoDate=%yy%-%mm%-%dd%"
GOTO :EOF
相关问题