在批处理文件中打印开始/结束时间和服务名称

时间:2017-07-10 11:23:19

标签: batch-file

我有一个停止服务的批处理文件,并在文件夹中创建.log文件。现在,在日志文件中应该有停止日期/开始日期和停止时间/开始服务时间和服务名称。我怎么能这样做?

@echo off
sc start tapisrv
if ERRORLEVEL 1 goto error
pause
sc stop tapisrv
if ERRORLEVEL 1 goto error
pause
if exist "C:\AliCanDirik" echo A file name AliCanDirik already exists in 
directory C:\
if not exist "C:\AliCanDirik" mkdir C:\AliCan
echo AliCanDirik is created in directory C:\

set logfile=C:\AliCanDirik\odev.log
echo Service name= tapisrv Date= %date% Time= %time% Username= %username% >> 
%logfile%
tapisrv -options oas >> %logfile%
pause

我改变了这个;

@echo off
sc start tapisrv
if ERRORLEVEL 1 goto error
set starttime=%time%
set startdate=%date%
pause
sc stop tapisrv
if ERRORLEVEL 1 goto error
set stoptime=%time%
set stopdate=%date%
pause
if exist C:\AliCanDirik (
    echo A file name AliCanDirik already exists in directory C:\
)else (
    mkdir C:\AliCanDirik
    echo AliCanDirik is created in directory C:\
)

set logfile=C:\AliCanDirik\odev.log
echo Service name= tapisrv Start Date= %startdate% Start Time= %starttime% 
Username= %username% >> %logfile%
echo Service name= tapisrv End Date= %stopdate% End Time= %stoptime% 
Username= %username% >> %logfile%

它会创建文件和.log文件,并在.log文件中获取时间和日期。但我需要分别学习开始时间和停止时间。 我想发现了它;

url(r'^(?P<name>)/$', employeedetail, name='employeedetail'),

暂停

2 个答案:

答案 0 :(得分:0)

嗯,这很容易。 %DATE%包含日期,%TIME%包含时间,服务显然称为tapisrv。您可以将这些信息组合在一起:ECHO %DATE% %TIME% tapisrv。要将其写入odev.log文件,只需添加>>odev.log即可。而已。但请注意:echo %UserName% > odev.log将始终“删除”旧文件并创建一个新文件。 >会覆盖该文件。如果要附加文本,则应使用>>代替。

@echo off
sc stop tapisrv
if ERRORLEVEL 1 goto error
pause
if exist "C:\AliCan" echo A file name AliCan already exists in
directory C:\
if not exist "C:\AliCan" mkdir C:\AliCan
echo AliCan is created in directory C:\
echo %UserName% > odev.log
ECHO %DATE% %TIME% tapisrv>>odev.log
pause

答案 1 :(得分:0)

我还不完全清楚你打算做什么。

此批处理使用if else以及上一个命令的成功&&或失败||的条件执行,类似于评估errorlevel。

@echo off
if exist "C:\AliCanDirik" (
  echo A file/folder name AliCanDirik already exists in directory C:\
) else (
  mkdir C:\AliCanDirik
  echo AliCanDirik is created in directory C:\
)
set "logfile=C:\AliCanDirik\odev.log"

sc start tapisrv && (
  >> "%logfile%" Echo started Service name=tapisrv Date=%date% Time=%time% Username=%username%
) || goto error
pause

sc stop tapisrv && (
  >> "%logfile%" Echo stopped Service name=tapisrv Date=%date% Time=%time% Username=%username%
) || goto error
pause

Rem afaik tapisrv is a dll, you can't run it this way.
>> "%logfile%" tapisrv -options oas
pause