首页 > 解决方案 > 将批处理脚本的输出重定向到日志文件

问题描述

下面的代码检查python版本并调用另一个python脚本来执行。我想把下面 bat 文件的所有日志,包括 python 脚本的输出,放到另一个日志文件中。做一些测试,我可以将日志放入文件中,但输出仍然打印命令打印窗口。我认为它会重定向输出。

setlocal
SET "parent=%~dp0"
set LOGFILE=%parent%\run.log
call :LOG > %LOGFILE% 2>&1

:LOG
set "$py=0"
set "$ms=0"

call:construct
for /f "delims=" %%a in ('python dele.py ^| findstr "2"') do set "$py=2"
for /f "delims=" %%a in ('python dele.py ^| findstr "3"') do set "$py=3"

del %parent%\dele.py

if %%py%% == 0 echo "python is not installed" & goto :EOF

echo running with python %py%
for /f "delims=" %%V in ('pip -V') do @set ver=%%V
echo %ver%
pip install --user -r %parent%\requirements.txt
[output on command prompt][1]
echo connectivity script is running...
python %parent%\ConnChk.py
echo connectivity check is completed
goto :EOF
:construct
echo import sys; print('{0[0]}.{0[1]}'.format(sys.version_info^)^)>dele.py

我不想在cmd上打印任何东西。有办法吗?任何帮助都会很棒!

[在 cmd 上输出][1] [1]:https://i.stack.imgur.com/qC4pZ.png

标签: batch-file

解决方案


我搞定了。

使用此链接:https ://superuser.com/questions/620865/dump-batch-script-output-into-a-text-file-without-specifing-batchfile-location-b

@echo off
REM setlocal enabledelayedexpansion

(
  content...
) > "%~dpn0.txt"

用开括号括起来的代码让它工作了!


推荐阅读