捕获“移动"输出.批量命令到变量中 [英] Capture the Output of "Move" Command into a variable in batch
问题描述
我正在尝试在批处理文件中运行移动命令.我已经使用它使它正常工作了……
I am trying to run a move command inside a batch file. I have pretty much made it worked fine using this...
Move /Y "%1\%2 %3" %4 >nul
:: and this for decision making and logging...
if %errorlevel%==0 GOTO DONE
if %errorlevel%==1 GOTO FAILED
注意:%1-4是我从另一个批处理文件传递过来的变量,它们基本上是文件的原始路径,文件名和新路径.
Note: %1-4 are variables i am passing over from another batch file that are basically the original path of the file, the file name and new path.
但是我想将命令的输出捕获到一个变量中,以便在同一批次中写入日志并做出决策...我指的是输出已移动1个文件.成功时或者找不到网络路径.当找不到文件时或与此相关的任何其他消息
But i would like to capture the output of the command into a variable for writing to a log and decision making within the same batch... The output i am referring to is the 1 file(s) moved. when successful or the The network path was not found. when it can't find the file or any other message for that matter
到目前为止,我已经尝试过了...
Up to this point i have tried this...
for /f "tokens=*" %%a in ('Move /Y "%1\%2 %3" %4') do set FailReason=%%a
即使此举仍然有效...我无法捕获上面列出的输出...
and even when the move still works... I can't capture the outputs listed above...
任何帮助将不胜感激.预先感谢.
Any help would be greatly appreciated. Thanks in advance.
推荐答案
这将使您将STDERR和STDOUT转换为变量:
This will let you get the STDERR as well as STDOUT into a variable:
@echo off
for /f "delims=" %%a in ('Move /Y "%1\%2 %3" %4 2^>^&1 ') do echo %%a
这篇关于捕获“移动"输出.批量命令到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!