以下是这些要求:
echo
)。我尝试过的操作(仅适用于1个通配符):
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
)
这将打印与a*.txt
或b*.txt
匹配的文件。我不希望这种扩张发生。我需要实际的通配符值才能进入循环。
通过扩展,我无法告诉用户某些通配符没有文件。例如。有文件a*.txt
,但没有文件b*.txt
。我需要告诉用户。像这样:
a * .txt:有文件。
b * .txt:没有文件,请检查。
类似的东西(这不起作用,只是我想要的想法):
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
)
)
基本上,我可以阻止%wildcard%
语句内IF
中值的扩展吗?
对于@ double-beep的评论:
您对多个IF EXIST
语句的想法正是我想要的,但是我不知道用户想要多少个通配符。
SET wildcard=a*.txt b*.txt [...]
REM this would be ok
IF EXIST a*.txt ( ... )
IF EXIST b*.txt ( ... )
[...]
但是,基于用户在通配符变量中输入的内容,我该如何灵活地进行操作?我想到了循环使用通配符的值,但是FOR进行了扩展,这是我不希望的。
答案 0 :(得分:1)
这段代码如何使用call
和参数,这些参数不能解析通配符:
@echo off
set "WildCard=a*.txt b*.txt *.vi"
call :LOOP %WildCard%
rem ...
goto :EOF
:LOOP
if "%~1"=="" goto :EOF
if exist "%~1" echo There are files that match the pattern: "%~1"
shift /0
goto :LOOP