windows - 如果找不到文件,如何避免创建输出文件?

标签 windows batch-file error-handling

我有一个批处理脚本,它询问路径并询问我要在该路径的文件夹和子文件夹中搜索的文件类型。然后它在 output.txt 文件中返回这些文件的路径。

这是我的代码:

@echo on
set LOGFILE=output.txt
set /P userInputPath=Enter the path you'd like to search?
set /p "FileType=Enter file type(s) here (ex: txt, pdf, docx): "

call :LOG > %LOGFILE%
exit
:LOG
for %%A in (%FileType%) do (dir /b /s %userInputPath%\*.%%A)
@pause

如果找不到文件或输入的路径错误,我想避免创建 output.txt 文件。谁能帮我解决这个问题。谢谢!

最佳答案

可以添加

for %%# in ("%LOGFILE%") do (
   if %%~z# equ 0 (
       del /s /q "%LOGFILE%"
   )
)

最后。它检查日志文件的大小是否为 0,如果是,则将其删除。

关于windows - 如果找不到文件,如何避免创建输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41813115/

相关文章:

windows - 在 .bat 脚本中运行所有命令

windows - 为什么我看不到在我的 Windows 程序中编译为二进制资源的图标?

c - 如何正确使用 CopyFileEx 和 CopyProgressRoutine 函数?

c++ - EOF 和 GETCHAR 结束循环

linux - .exe 文件如何与 Windows 内核交互? Linux .bin 文件相同

python - Python3 : creating error for input of negative

batch-file - 如何在没有外部工具的情况下在命令提示符下创建唯一的临时文件路径?

batch-file - 如何使用批处理命令关闭特定文件

java - 如何更改内部服务器错误的处理?

php - 在我的webapp中存储异常?