Batch file 如果存在多个通配符
这些是要求:Batch file 如果存在多个通配符,batch-file,Batch File,这些是要求: SET wildcard=a*.txt cd c:\somedirectory IF EXIST "%wildcard%" ( REM PROCESS THE FILES ) ELSE ( ECHO There are no files that match that wildcard, please review. ) SET wildcard=a*.txt b*.txt cd c:\somedirectory FOR %%A IN (%wildcard%) D
SET wildcard=a*.txt
cd c:\somedirectory
IF EXIST "%wildcard%" (
REM PROCESS THE FILES
) ELSE (
ECHO There are no files that match that wildcard, please review.
)
SET wildcard=a*.txt b*.txt
cd c:\somedirectory
FOR %%A IN (%wildcard%) DO (
ECHO %%A
)
SET wildcard=a*.txt b*.txt c*.txt
cd c:\somedirectory
REM loop on the wildcards
FOR %%A IN (%wildcard%) DO (
REM verify if there are files for that wildcard
IF EXIST %%A (
REM loop on the files from the specific wildcard
FOR %%F IN (%%A) DO (
REM PROCESS THE FILES
)
) ELSE (
ECHO This pattern %%A has no files associated
)
)
- 指定要检入变量的通配符
- 对于每个通配符,验证是否存在文件。如果没有,则警告用户(只需一个
)回音
- 处理结果文件
SET wildcard=a*.txt
cd c:\somedirectory
IF EXIST "%wildcard%" (
REM PROCESS THE FILES
) ELSE (
ECHO There are no files that match that wildcard, please review.
)
SET wildcard=a*.txt b*.txt
cd c:\somedirectory
FOR %%A IN (%wildcard%) DO (
ECHO %%A
)
SET wildcard=a*.txt b*.txt c*.txt
cd c:\somedirectory
REM loop on the wildcards
FOR %%A IN (%wildcard%) DO (
REM verify if there are files for that wildcard
IF EXIST %%A (
REM loop on the files from the specific wildcard
FOR %%F IN (%%A) DO (
REM PROCESS THE FILES
)
) ELSE (
ECHO This pattern %%A has no files associated
)
)
显然,只有当通配符是唯一的时,这才有效
我尝试过的:
SET wildcard=a*.txt
cd c:\somedirectory
IF EXIST "%wildcard%" (
REM PROCESS THE FILES
) ELSE (
ECHO There are no files that match that wildcard, please review.
)
SET wildcard=a*.txt b*.txt
cd c:\somedirectory
FOR %%A IN (%wildcard%) DO (
ECHO %%A
)
SET wildcard=a*.txt b*.txt c*.txt
cd c:\somedirectory
REM loop on the wildcards
FOR %%A IN (%wildcard%) DO (
REM verify if there are files for that wildcard
IF EXIST %%A (
REM loop on the files from the specific wildcard
FOR %%F IN (%%A) DO (
REM PROCESS THE FILES
)
) ELSE (
ECHO This pattern %%A has no files associated
)
)
这将打印与a*.txt
或b*.txt
匹配的文件。我不希望这种扩张发生。我需要实际的通配符值才能进入循环
通过扩展,我无法告诉用户他的某些通配符没有文件。例如,有文件a*.txt
,但没有文件b*.txt
。我需要告诉用户。比如:
a*.txt:有文件。b*.txt:没有文件,请查看 类似(这不起作用,只是我想要的想法):
SET wildcard=a*.txt
cd c:\somedirectory
IF EXIST "%wildcard%" (
REM PROCESS THE FILES
) ELSE (
ECHO There are no files that match that wildcard, please review.
)
SET wildcard=a*.txt b*.txt
cd c:\somedirectory
FOR %%A IN (%wildcard%) DO (
ECHO %%A
)
SET wildcard=a*.txt b*.txt c*.txt
cd c:\somedirectory
REM loop on the wildcards
FOR %%A IN (%wildcard%) DO (
REM verify if there are files for that wildcard
IF EXIST %%A (
REM loop on the files from the specific wildcard
FOR %%F IN (%%A) DO (
REM PROCESS THE FILES
)
) ELSE (
ECHO This pattern %%A has no files associated
)
)
基本上,我可以防止在IF
语句中扩展%wildcard%
中的值吗
对于@double beep的评论:
- 您关于多个
语句的想法正是我想要的,但我不知道用户需要多少通配符IF-EXIST
SET wildcard=a*.txt b*.txt [...] REM this would be ok IF EXIST a*.txt ( ... ) IF EXIST b*.txt ( ... ) [...]
但是,如何根据用户在wilcard变量中的输入灵活地实现这一点呢?我曾想过在通配符的值上循环,但为了扩展,我不想这样做。这段代码如何,使用
调用
,参数不解析通配符:
@echo关闭
设置“通配符=a*.txt b*.txt*.vi”
调用:循环%通配符%
雷姆。。。
后藤:EOF
:循环
如果“%~1”==”转到:EOF
如果存在“%$1”,则存在与模式“%$1”匹配的文件
移位/0
转到:循环
键入以获取/?
并查看/f
。这太复杂了,无法在注释中写入,但类似于如果存在a*.txt如果存在b*.txt如果存在c*.txt
,但是您将需要多个否则
s。否则,您可以在后添加一个转到
,回显此模式%%a没有关联的文件
,然后转到自定义子例程。我还是不确定:你是什么意思?只需检查是否存在多个通配符,以及它们是否不起作用?这里有什么问题?使用REM
而不是:
。此外,使用SET wildcard=a*.txt b*.txt
get it-JosefZ,我将这样做,并编辑问题。谢谢@双重嘟嘟声:我在问题中添加了一些细节,以解决您的if
声明的想法。非常好!我没有想到要使用shift
,这非常好用!我喜欢这个网站和这里的社区。谢谢