批处理文件以验证文件夹中是否存在任何文件,然后向用户显示一条消息 [英] Batch file to verify existence of any files in folder and then present user a message

查看:78
本文介绍了批处理文件以验证文件夹中是否存在任何文件,然后向用户显示一条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要验证文件夹中是否存在任何文件,是否向用户显示一条消息.

I need to verify if any files exist in a folder and if so present the user with a message.

目前我有这个:

IF EXIST C:\PLUS\ADMIN\BATCH\*.* (
start "" cmd/c "echo Files in the directory! &echo (&pause
)
Exit

我已经花了数小时阅读我在变量和管道结果中挖掘出的内容,但是我是一个完整的批处理文件新手,所以我真的希望有人可以告诉我我做错了什么.

I've spent hours reading the things I've dug up on variables and piping results to things but I'm a complete batch file newbie so I'm really hoping someone can just tell me what I'm doing wrong.

当前,批处理文件运行得很好,但是无论目录中是否有文件,它都会在屏幕上显示该消息.这些文件通常被命名为20141010.570、20141011.571等,其文件扩展名基于不断增加的数字(因此,使用* .999完成后,它将扩展为4位数字)

Currently the batch file runs just fine but it's throwing up the message on the screen regardless of whether there are files in the directory or not. Those files tend to be named 20141010.570, 20141011.571, etc. with variable file extensions based upon an ever increasing number (so it'll stretch into 4 digits once it's done with *.999)

推荐答案

您的代码存在的问题是,在Windows中,所有文件夹都至少包含两个文件夹(...),并且测试if exist c:\somewhere\*将始终是真实的.

The problem with your code is that in windows all folders contain at least two folders (. and ..) and the test if exist c:\somewhere\* will always be true.

一个简单的解决方案是使用dir命令,要求仅显示文件,不显示目录,并查看是否引发错误

One easy solution is to use dir command asking to only show the files, without directories, and see if it raises an error

dir /a-d "C:\PLUS\ADMIN\BATCH\*" >nul 2>nul && (
   start "" cmd /c "@echo Files in the directory! &@echo(&@pause
) || ( 
   echo there are no files
)

/a-d将排除文件夹.如果有文件,则未设置errorlevel并执行&&之后的代码.否则,如果没有文件,则dir命令失败,设置错误级别并执行||之后的代码.

The /a-d will exclude folders. If there are files, errorlevel is not set and the code after && is executed. Else, if there are no files, dir command fails, errorlevel is set and the code after || is executed.

这篇关于批处理文件以验证文件夹中是否存在任何文件,然后向用户显示一条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆