Batch file 批处理文件,以验证文件夹中是否存在任何文件,然后向用户显示一条消息

Batch file 批处理文件,以验证文件夹中是否存在任何文件,然后向用户显示一条消息,batch-file,Batch File,我需要验证文件夹中是否存在任何文件,如果存在,则向用户显示一条消息 目前我有: IF EXIST C:\PLUS\ADMIN\BATCH\*.* ( start "" cmd/c "echo Files in the directory! &echo (&pause ) Exit 我花了好几个小时阅读我从变量和管道结果中挖掘出来的东西,但我是一个完整的批处理文件新手,所以我真的希望有人能告诉我我做错了什么 当前批处理文件运行正常,但不管目录中是否有文件,它都会在屏幕上弹出消息。

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

目前我有:

IF EXIST C:\PLUS\ADMIN\BATCH\*.* (
start "" cmd/c "echo Files in the directory! &echo (&pause
)
Exit
我花了好几个小时阅读我从变量和管道结果中挖掘出来的东西,但我是一个完整的批处理文件新手,所以我真的希望有人能告诉我我做错了什么

当前批处理文件运行正常,但不管目录中是否有文件,它都会在屏幕上弹出消息。这些文件往往被命名为20141010.570、20141011.571等,其文件扩展名根据不断增加的数量而变化(因此,一旦使用*.999,它将扩展为4位数字)


代码的问题是,在windows中,所有文件夹都至少包含两个文件夹(
),并且测试
如果存在c:\where\*
将始终为真

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

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命令失败,设置错误级别并执行
|
之后的代码。

由于所有文件都从2014开始,因此您可以使用以下命令:

IF EXIST "C:\PLUS\ADMIN\BATCH\2*.*" (
   echo Files are in the directory!
   echo(
   pause
)
Exit

是否所有文件都是从2014年开始的,因此以
2
开头?是的,它们总是以年份作为文件名的开头。谢谢!!!!这就解决了问题。唯一的修正是我需要在cmd和/c之间插入一个空格,否则它就不起作用了。真让人松了一口气,我还以为我是疯了呢@迈克,对不起,我没有用try/copy/paste,而是直接打出来的,没有找到空格。现在更正。
&    seperates commands on a line.

&&    executes this command only if previous command's errorlevel is 0.

||    (not used above) executes this command only if previous command's errorlevel is NOT 0

>    output to a file

>>    append output to a file

<    input from a file

|    output of one command into the input of another command

^    escapes any of the above, including itself, if needed to be passed to a program

"    parameters with spaces must be enclosed in quotes

+ used with copy to concatinate files. E.G. copy file1+file2 newfile

, used with copy to indicate missing parameters. This updates the files modified date. E.G. copy /b file1,,

%variablename% a inbuilt or user set environmental variable

!variablename! a user set environmental variable expanded at execution time, turned with SelLocal EnableDelayedExpansion command

%<number> (%1) the nth command line parameter passed to a batch file. %0 is the batchfile's name.

%* (%*) the entire command line.

%<a letter> or %%<a letter> (%A or %%A) the variable in a for loop. Single % sign at command prompt and double % sign in a batch file.


.
--
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
)
IF EXIST "C:\PLUS\ADMIN\BATCH\2*.*" (
   echo Files are in the directory!
   echo(
   pause
)
Exit