将命令附加到CMD脚本中的文件

时间:2014-11-22 22:23:21

标签: batch-file cmd command output echo

这看起来很愚蠢但是,我需要添加像文本字符串这样的指令,但是添加其他更少的行。

tasklist /fi "SessionName eq services" | find /I "Tomcat" | find /I ".exe"

我尝试:

@echo off
set "IniCatching=%date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,5%"
set theFile=%~n0_%IniCatching%.txt
set "tasklistecho=echo tasklist /fi ^"SessionName eq services^" ^| find /I ^"Tomcat^" ^| find /I ^".exe^""
echo Before>>%theFile%
call %tasklistecho%>>%theFile%
echo After>>%theFile%

但是,这似乎是尝试显示结果(不会被视为字符串文本else命令)。

其他形式:

@echo off
set "IniCatching=%date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,5%"
set theFile=%~n0_%IniCatching%.txt
set "tasklistinst=tasklist /fi ^"SessionName eq services^" ^| find /I ^"Tomcat^" ^| find /I ^".exe^""
echo "echo..">>%theFile%
echo tasklist:>>%theFile%
echo "inst..">>%theFile%
echo %tasklistinst%>>%theFile%


I have in the file (wServ_wFiles_20141122-170025.07.txt):

"echo.."
tasklist:
"inst.."

In my prompt (not in my file) I have:

tasklist /fi "SessionName eq services" | find /I "Tomcat" | find /I ".exe">>wServ_wFiles_20141122-170025.07.txt

Like you see, the value and ">>" filename is treated like only one String....

当我尝试

echo "%tasklistinst%">>%theFile%

我有这个:

FIND: Parameter format not correct

请帮助......

我想在我的文件中包含我的命令......

2 个答案:

答案 0 :(得分:1)

您是否在没有echo的情况下尝试:

%tasklistinst%>>%theFile%

答案 1 :(得分:0)

为什么将字符串存储在变量中?这不是必需的,通常最简单的方法是更好:

@echo off
set "IniCatching=%date:~0,4%%date:~5,2%%date:~8,2%-%time:~0,2%%time:~3,2%%time:~6,5%"
set theFile=%~n0_%IniCatching%.txt
(
echo Before
echo tasklist /fi "SessionName eq services" ^| find /I "Tomcat" ^| find /I ".exe"
echo After
) >> "%theFile%"