报告无法删除在DEL批处理文件的文件 [英] Reporting failed to delete to a file in DEL batch files
问题描述
怎样才能创建一个报表文件,其中一个DEL命令未能正确执行?
How does one create a report file where a DEL command failed to execute properly?
我还以为你会怎么做:
DEL FILENAME.TXT ECHO "FILENAME.TXT" >> REPORT.TXT
但它的作用是使一个空的报告文件,无论是否发现FILENAME.TXT与否。我想创造一个失败,如果有删除FILENAME.TXT不管什么原因,在弹出的消息被倒入REPORT.TXT的报告文件。
but all it does is make an empty report file, regardless of if it finds FILENAME.TXT or not. I'm wanting to create a report file where if it failed to delete FILENAME.TXT for whatever reason that the message that pops up is dumped into REPORT.TXT.
任何想法?
推荐答案
即使没有找到文件DEL命令总是返回错误code 0,或者如果文件需要管理员权限,所以你需要检查,如果文件仍然存在试图删除后:
The Del command always returns errorcode 0 even if file is not found or if file need admin access, so you need to check if file still exist after trying to delete it:
@Echo OFF
Set "File=File.txt"
Del "%File%" 2>NUL & If exist "%File%" (
Echo [+] File failed to delete: "%File%" >> "Report.txt"
)
Pause&Exit
有关的大量文件,你可以做一个程序,不写一个大code,是这样的:
For a large amount of files you can make a procedure to not write a large code, like this:
@Echo OFF
Call :ReportDelete "C:\File1.txt"
Call :ReportDelete "C:\File2.txt"
Call :ReportDelete "C:\File3.txt"
Call :ReportDelete "C:\File4.txt"
Call :ReportDelete "C:\File5.txt"
Pause&Exit
:ReportDelete
(Del "%~1" 2>NUL & If exist "%~1" (Echo [+] File failed to delete: "%~1" >> "Report.txt")) & (Goto:EOF)
这篇关于报告无法删除在DEL批处理文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!