0

我正在尝试在批处理文件中运行移动命令。我已经用这个让它工作得很好......

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 是我从另一个批处理文件传递过来的变量,它们基本上是文件的原始路径、文件名和新路径。

但是我想将命令的输出捕获到一个变量中,以便在同一批次中写入日志和决策……我指的输出是移动的 1 个文件。成功时或找不到网络路径。当它找不到文件或任何其他消息时

到目前为止,我已经尝试过这个......

for /f "tokens=*" %%a in ('Move /Y "%1\%2 %3" %4') do set FailReason=%%a

即使移动仍然有效......我无法捕获上面列出的输出......

任何帮助将不胜感激。提前致谢。

4

2 回答 2

1

这将使您可以将 STDERR 和 STDOUT 放入变量中:

@echo off
for /f "delims=" %%a in ('Move /Y "%1\%2 %3" %4 2^>^&1 ') do echo %%a
于 2013-10-03T06:27:20.600 回答
0
@ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"

DEL "%destdir%\test*">NUL 2>nul
FOR /l %%i IN (1,1,4) DO COPY /y NUL "%sourcedir%\testfile %%i.txt" >nul
COPY /y NUL "%destdir%\testfile 2.txt" >NUL
COPY /y NUL "%destdir%\testfile 4.txt" >NUL
ATTRIB +r "%destdir%\testfile 4.txt"

:: 5 tests - OK, destfile exists, destfile R/O, sourcefile not found, sourcedir does not exist


CALL :testmove %sourcedir% testfile 1.txt %destdir%
CALL :testmove %sourcedir% testfile 2.txt %destdir%
CALL :testmove %sourcedir% testfile 4.txt %destdir%
CALL :testmove %sourcedir% testfile 5.txt %destdir%
CALL :testmove \unknowndir testfile 5.txt %destdir%

ATTRIB -r "%destdir%\testfile 4.txt"
GOTO :eof

:testmove
FOR /f "delims=" %%i IN ('Move /Y "%1\%2 %3" %4 2^>^&1') DO SET reason=%%i
ECHO %* produced ERRORLEVEL %ERRORLEVEL% and reason %reason%

GOTO :EOF

ERRORLEVEL虽然总是看起来是 0...

于 2013-10-03T06:03:23.833 回答