批处理文件和DEL错误级别0问题 [英] Batch file and DEL errorlevel 0 issue
问题描述
批处理有权从特定位置和输出成功或标准输出/标准错误信息的文件和目录删除到一个新的.txt文件。我创建了大部分的剧本,并准确执行,因为它应该,除非删除成功向前移动到下一行,而不是在日志回声成功的消息。
The batch has to remove files and directories from specific locations and output success or stdout/stderr messages to a new .txt file. I have created the most of the script and it performs exactly as it should, except when the deletion is successful it moves forward to the next line rather than echo a 'successful' message on the log.
echo Basic Deletion Batch Script > results.txt
@echo off
call :filelog >> results.txt 2>&1
notepad results.txt
exit /b
:filelog
call :delete new.txt
call :delete newer.txt
call :delete newest.txt
call :remove c:\NoSuchDirectory
GOTO :EOF
:delete
echo deleting %1
del /f /q c:\Users\newuser\Desktop\%1
if errorlevel 0 echo succesful
GOTO :EOF
:remove
echo deleting directory %1
rmdir /q /s %1
GOTO :EOF
有关某种原因,我无法找到语法如果德尔成功回声成功。在上面的例子,如果我删除行
For some reason I can't find the syntax for if del succeeds echo 'successful'. In the above example if I remove the line
if errorlevel 0 echo successful
一切工作正常,但没有成功的消息。有了这条线留在呼应每一行的成功。
Everything works fine, but no success message. With this line left in it echoes success for every line.
推荐答案
的删除
命令不设置的ErrorLevel
只要给定的参数是有效的,它甚至复位的ErrorLevel
到 0
在这种情况下(至少在Windows 7中)。
del
and ErrorLevel
?
The del
command does not set the ErrorLevel
as long as the given arguments are valid, it even resets the ErrorLevel
to 0
in such cases (at least for Windows 7).
删除
修改的ErrorLevel
只的情况下,提供了一个无效的开关(德尔/ X
套的ErrorLevel
到 1
),没有参数在所有(<$ C指定$ C>删除套的ErrorLevel
到 1
太),或不正确的文件路径给定(删除
套的ErrorLevel
到 123
),在至少为Windows 7。
del
modifies the ErrorLevel
only in case an invalid switch is provided (del /X
sets ErrorLevel
to 1
), no arguments are specified at all (del
sets ErrorLevel
to 1
too), or an incorrect file path is given (del :
sets ErrorLevel
to 123
), at least for Windows 7.
一个可能的解决办法是捕捉到的
,因为在删除错误的情况下,相关信息( STDERR
输出德尔找不到[...]
,访问被拒绝。
,因为它正由另一个进程的进程无法访问该文件。
)写入那里。这种看起来像:
A possible work-around is to capture the STDERR
output of del
, because in case of deletion errors, the related messages (Could Not Find [...]
, Access is denied.
, The process cannot access the file because it is being used by another process.
) are written there. Such might look like:
for /F "tokens=*" %%# in ('del /F /Q "\path\to\the\file_s.txt" 2^>^&1 1^> nul') do (2> nul set =)
要在批处理文件中使用命令提示符code,而不是直接写%#
而不是 %%#的code>。
To use the code in command prompt directly rather than in a batch file, write %#
instead of %%#
.
如果你不想删除只读文件,删除 /˚F
从删除
命令行;结果
如果你想提示(如果通配符?
和/或 *
是文件路径present) ,删除 / Q
。
If you do not want to delete read-only files, remove /F
from the del
command line;
if you do want prompts (in case wildcards ?
and/or *
are present in the file path), remove /Q
.
这执行命令行 DEL / F / Q\\路径\\为\\中\\ file_s.txt
。由部分 2 - ;&放大器; 1 1 GT; NUL
在 STDOUT
命令输出将被解雇,其 STDERR
输出将是重定向使 FOR / F
接收它。
This executes the command line del /F /Q "\path\to\the\file_s.txt"
. By the part 2>&1 1> nul
, the command output at STDOUT
will be dismissed, and its STDERR
output will be redirected so that for /F
receives it.
如果删除成功,删除
不产生 STDERR
输出,因此 FOR / F
循环不重复,因为没有进行解析。请注意,的ErrorLevel
不会在这种情况下进行复位,其值不变。
If the deletion was successful, del
does not generate a STDERR
output, hence the for /F
loop does not iterate, because there is nothing to parse. Notice that ErrorLevel
will not be reset in that case, its value remains unchanged.
如果 FOR / F
临危从删除$ C任何
STDERR
输出$ C>命令行,在循环体的命令被执行,这是设置=
;这是一个无效的语法,因此集
设置的ErrorLevel
到 1
。在 2 - ; NUL
部分避免了信息命令的语法不正确。
来显示。
If for /F
recieves any STDERR
output from the del
command line, the command in the loop body is executed, which is set =
; this is an invalid syntax, therefore set
sets the ErrorLevel
to 1
. The 2> nul
portion avoids the message The syntax of the command is incorrect.
to be displayed.
要设置的ErrorLevel
明确你也可以使用 CMD / C出口/ B 1
。或许,这条线更为清晰可辨。可以肯定的是更加灵活,因为你可以说出任何(符号32位)数,包括 0
来清除它(省略若干清除它)。它可能在性能方面,虽然是有点差。
To set the ErrorLevel
explicitly you could also use cmd /C exit /B 1
. Perhaps this line is more legible. For sure it is more flexible because you can state any (signed 32-bit) number, including 0
to clear it (omitting the number clears it as well). It might be a bit worse in terms of performance though.
下面的批处理文件演示了如何在上述工作围绕可应用于:
The following batch file demonstrates how the above described work-around could be applied:
:DELETE
echo Deleting "%~1"...
rem this line resets ErrorLevel initially:
cmd /C exit /B
rem this line constitutes the work-around:
for /F "tokens=*" %%# in ('del /F /Q "C:\Users\newuser\Desktop\%~1" 2^>^&1 1^> nul') do (2> nul set =)
rem this is the corrected ErrorLevel query:
if not ErrorLevel 1 echo Deleted "%~1" succesfully.
goto :EOF
presetting 的ErrorLevel
除了上述的命令 CMD / C出口/ B
,你也可以使用&GT; NUL版本
重置的ErrorLevel
。这可以用 FOR / F
循环工作围绕这样的组合:
Presetting ErrorLevel
Besides the above mentioned command cmd /C exit /B
, you can also use > nul ver
to reset the ErrorLevel
. This can be combined with the for /F
loop work-around like this:
> nul ver & for /F "tokens=*" %%# in ('del /F /Q "\path\to\the\file_s.txt" 2^>^&1 1^> nul') do (2> nul set =)
替代方法,无需 FOR / F
而不是使用 FOR / F
来捕捉 STDERR
输出OD 删除
的找到
命令也可以使用像查找/ V
,它返回一个的ErrorLevel
的 1
1 其它>
Alternative Method Without for /F
Instead of using for /F
to capture the STDERR
output od del
, the find
command could also be used like find /V ""
, which returns an ErrorLevel
of 1
if an empty string comes in and 1
otherwise:
del "\path\to\the\file_s.ext" 2>&1 1> nul | find /V "" 1> nul 2>&1
不过,这会返回一个的ErrorLevel
1
的情况下,删除已成功与<$ C $的C> 0 。为了扭转这种行为,一个如果
/ 其他
子句可以附加这样的:
However, this would return an ErrorLevel
of 1
in case the deletion has been successful and 0
if not. To reverse that behaviour, an if
/else
clause could be appended like this:
del "\path\to\the\file_s.ext" 2>&1 1> nul | find /V "" 1> nul 2>&1 & if ErrorLevel 1 (1> nul ver) else (2> nul set =)
这篇关于批处理文件和DEL错误级别0问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!