akinuri
akinuri

Reputation: 12027

How to check file extension?

I have a small batch script to remove zone identifiers [SO] from URL files (Internet Shortcut). I've improved it to work with multiple files/folders. I just select the files/folders and drop them on the batch script, and the script removes the zone identifiers.

When looping a folder's contents, there seems to be a convenient way to target specific files, e.g. FOR %%j IN (%%i\*.url) for URL files, but if I have only a list of files, how do I check if the file is a URL file, that is its extension is .url?

I've found a way [ss64] to return the extension %%~xi (returns .url; i is the loop variable), but couldn't make it work.

REM loop arguments
FOR %%i IN (%*) DO (
    REM check if the argument is a directory
    IF EXIST %%~si\NUL (
        REM loop children (filtering url files)
        FOR %%j IN (%%i\*.url) DO (
            echo %%j
            REM remove zone identifier
            echo. > %%j:Zone.Identifier
        )
    ) ELSE (
        REM check if the argument is a url file    <<< how?

        REM returns the extension: .url
        echo %%~xi

        REM doesn't work. why?
        IF %%~xi == ".url" (
            echo %%i
            echo. > %%i:Zone.Identifier
        )

    )
)

Doing simply IF %%~xi == ".url" doesn't work. I'm not that familiar with batch scripts, so I must be missing something. How do I fix this?

Upvotes: 0

Views: 1998

Answers (1)

epanalepsis
epanalepsis

Reputation: 935

Seems just like a little typo.

It should work like this:

IF "%%~xi" == ".url" 

Upvotes: 2

Related Questions