batch-file - 将批处理脚本的输出重定向到日志文件
问题描述
下面的代码检查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
解决方案
我搞定了。
@echo off
REM setlocal enabledelayedexpansion
(
content...
) > "%~dpn0.txt"
用开括号括起来的代码让它工作了!
推荐阅读
- spring-boot - ErrorWebFluxAutoConfiguration 不尊重 ErrorWebExceptionHandler 类型的 Application 中的 bean
- javascript - Moment js 跨时区的日期值比较
- typescript - 是否可以使用联合 @Field 创建 @ArgType
- game-maker - 跳转缓冲区不能很好地与变量跳转
- javascript - 用户登录后如何在passport.authenticate(本地)中返回用户对象?
- c# - 在一个线程中发送数据是错误的吗?
- vue.js - 我们是否应该将 mixin 转换为 Vue 3 中基于类的组件定义语法?
- wordpress - 如何获取角色等于无的所有用户的列表
- python-3.x - 嵌套 FOR 循环为大字符串值提供超时错误 - Python
- mongodb - MongoDB select distinct where not in select distinct