windows - Batch file ignores ELSE statement inside a FOR loop -
i creating batch file decompresses archives detect in specified folder. right trying detect archive files within specified folder using code:
@echo off setlocal setlocal enabledelayedexpansion set quote=" set cwdtext=current working directory: set cwd=%cd% set fullcwd=%cwdtext%%cwd% set path=%~dp0 set types=file_types.txt set fullpath=%path%%types% echo %fullcwd% echo %fullpath% :specifypath echo: set /p directory=specify full path location of archive files: if "%cd%"=="%directory%" ( echo specified location current directory goto specifypath ) else ( pushd %directory% cd goto checkarchives ) :checkarchives %%t in (*.zip,*.rar,*.7z) ( if exist %%t ( echo archive files detected goto eof ) else ( echo archive files not detected goto eof ) goto eof ) :extract echo extracting archive files :eof pause
the part wherein start checking archive files @ :checkarchives
label. problem i'm having right whenever try specify folder wherein there no archive files, code jumps :extract
label , not go else
statement inside for
statement. can explain why so?
i tried using if not exist...
statement after first if
statement still same behavior.
when element being enumerated for
command doesn't contain wildcard, code in do
clause executed , for
replaceable parameter contain element is.
but when element being enumerated contains wildcard assumed needs expanded , file system checked presence of files matching wildcard expression. if there not files matching, code in do
clause not executed element. if there files matching expression, code in do
clause executed not expression, each matching file, , for
replaceable parameter hold reference current one.
as code using wildcards if there matching files, code in do
clause executed each of files (in case first file found because goto
), and, if there not matching file, code in do
clause not executed else
clause can not reached.
but, lets suppose for
not check file system, , elements indicated iterated written. have logic error. inner if
command check existence of files matching first set. if not files matching first set found, leaving for
command goto
inside else
clause, so, rest of sets not checked.
to keep for
check existence of files better way be
for %%t in (*.zip, *.rar, *.7z) goto :extract echo no archives found goto eof
if file, matching of wildcard expressions, found goto
cancel current enumeration , jump :extract
label. if for
loop ends , next line reached because no matching file has been found.
Comments
Post a Comment