Mohit Yadav
Mohit Yadav

Reputation: 41

Difference between %~2 and %2 in batch programming?

I am studying batch programming and i cant literally distinguish between %~2 and %2 .Also mention an example please.

Upvotes: 2

Views: 9634

Answers (3)

Duncan
Duncan

Reputation: 95712

%2 substitutes in the second argument. %~2 substitutes the second argument but removes any quote marks:

C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2

C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second

C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third

At the command prompt, type help for to find the options when expanding variables (which mostly work even if not in a for command):

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

    %~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.

Upvotes: 4

MichaelS
MichaelS

Reputation: 6042

Well, that's simple. %2 is the second parameter passed to your bat file. Adding ~ will remove quotes from your variable.

Consider the following short bat file (test.bat):

@ECHO OFF
ECHO %2
ECHO %~2

Wenn you call your bat file with test.bat p1 p2 it will output:

p2

p2

Now try test.bat p1 "p2"! this time the output will be:

"p2"

p2

So if you don't have any quotes the output will be the same and if you do have some %2 will keep them and %~2 will remove them.

Upvotes: 2

xuma202
xuma202

Reputation: 1084

%2 is the second argument passed to the batch file.

myfile.bat firstArg secondArg

because arguments are often file paths there is some extra syntax to extract parts of the path.

%~2 removes any "" on the second argument.

%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 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    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

possible duplicate: What does %~d0 mean in a Windows batch file?

Upvotes: 1

Related Questions