首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脚本批处理日志文件

脚本批处理日志文件
EN

Stack Overflow用户
提问于 2020-10-15 22:15:44
回答 1查看 105关注 0票数 1
代码语言:javascript
复制
@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

我有上面的批处理文件,它调用多个bat文件。我已经测试过这个脚本,它运行得很好。

我唯一的问题是生成的日志文件包含所有信息,而且它是一个大文件。

是否可以只记录注释和回显,并排除在屏幕上执行的内容?

例如,我不希望一个文件在日志文件中显示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-17 16:03:15

我已经使用了您的代码并重新编写它来使用泛型函数,这些函数将代码重复到一个更容易管理的表单中,并允许您根据需要通过编辑变量列表向FTP和脚本部分添加或删除任何步骤。

我也只返回不包括“文件移动”的输出。

或者,如果您真的只希望状态行打印,您可以将其更改为将这些信息打印到日志,而不是将所有信息打印到日志(这是因为在调用其他步骤时将重定向放到日志中。

而且,这是写之前,它实际上是通过所有的goto命令,而不需要调用每个在顶部。

这是重构的代码,我还没有测试过它,但是应该是好的,我必须走几个小时,你可以问任何问题,我会很乐意回答当我有时间。

代码语言:javascript
复制
@( 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64380396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档