0

What does "%%~xi" means in batch script command?

for %%i in (%1) do set ext="%%~xi"

I know that its a for loop taking 1 command line argument, "~" is NOT and "i" is the variable, but "x"? And what does this command do?

2
  • The ~ in batch means for %%i variable and also cmd line %1 argument modifier See help for and help call Commented Jul 24, 2017 at 10:44
  • 1
    See this... Commented Jul 24, 2017 at 11:16

3 Answers 3

2

From FOR /?:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.


In a batch file as opposed to the cmd.exe window, you would double the % characters.

Sign up to request clarification or add additional context in comments.

Comments

0

%%~xi gives you the file extension. If %%i is "somefile.txt" then %%~xi will return .txt.

Comments

0

Reference: https://ss64.com/nt/syntax-args.html

Parameter Extensions

    When an argument is used to supply a filename then the following extended syntax can be applied:

    we are using the variable %1 (but this works for any parameter)

    %~f1 Expand %1 to a Fully qualified path name - C:\utils\MyFile.txt

    %~d1 Expand %1 to a Drive letter only - C:

    %~p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

    %~n1 Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash\) - the last folder in that path.

    %~x1 Expand %1 to a file eXtension only - .txt

    %~s1 Change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists.)

    %~1   Expand %1 removing any surrounding quotes (")

    %~a1 Display the file attributes of %1

    %~t1 Display the date/time of %1

    %~z1 Display the file size of %1

    %~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

    %~dp1 Expand %1 to a drive letter and path only

    %~sp1 Expand %1 to a path shortened to 8.3 characters

    %~nx2 Expand %2 to a file name and extension only

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.