Question

i check the extension of a file (.iso or .img):

set extension=%~x1
set typefile=0
FOR %%a IN (.iso .img) DO (
    if %%a==%extension% set /a typefile=!typefile!+1
)

if %typefile%==0 (
    GOTO NOMOUNT
) else (
    GOTO MOUNT
)

its works, but the problem is when the file have a filename like this for example:

9600.16384.130821-1623_x64fre_Client_IT-IT-IRM_CCSA_DV5.ISO 

in this case the %typefile% is set=0 also if the file is an iso.

Was it helpful?

Solution

change if %%a to if /i %%a which makes the string-match case-insensitive.

BTW, set /a typefile=!typefile!+1 could just as easily be set /a typefile+=1

OTHER TIPS

Your immediate problem is NOT related to long filenames, but to case-sensitivity when comparing strings - see @Magoo's answer.

However, overall it's not entirely clear what you're trying to achieve; for instance, your for loop enumerates string literals - .iso and .img - rather than trying to match filenames in the current folder with patterns *.iso and *.img.

The following solution assumes that you want to count all files in the current folder whose filename extension case-insensitively matches the extension passed in as the 1st argument (including the initial .):

 REM The extension to match; specify _with_ initial `.`, e.g. `.iso`
 REM `~x` ensures that if a whole filename was specified, only the extension
 REM is extracted.  
set extension=%~x1

set typefile=0
FOR %%a IN (*%extension%) DO (
    set /a typefile+=1
)

if %typefile% EQU 0 (
    GOTO NOMOUNT
) else (
    GOTO MOUNT
)

Note:

  • Using a wildcard pattern such as *.iso always performs case-INsensitive matching; e.g., *.iso will match both files named f1.iso and f2.ISO.
  • When comparing numbers for equality, EQU is better than ==.
  • Using set /a inside a for loop works even if setlocal enabledelayedexpansion is NOT in effect.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top