0

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?

akinuri
  • 10,690
  • 10
  • 65
  • 102

1 Answers1

2

Seems just like a little typo.

It should work like this:

IF "%%~xi" == ".url" 
Gerhard
  • 22,678
  • 7
  • 27
  • 43
epanalepsis
  • 853
  • 1
  • 9
  • 26
  • _*facepalm*_ Indeed, it does! :) – akinuri Nov 13 '19 at 09:05
  • I just dropped a bunch of files and folders (together) on the batch file, and it correctly printed the URL files (skipped other files: pdf, txt, etc.). Quoting the `"%%~xi"` did the trick. – akinuri Nov 13 '19 at 09:15
  • 3
    @akinuri: change `"%~x1"` (extension of first parameter) to `"%%~xi" (extension of the filename hold by the `for`-variable `%%i`)` to make it correct. – Stephan Nov 13 '19 at 09:18