Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does %~1 do in this batch file?

Tags:

I found this code, but there are parts that I do not understand.

This is my code:

Main.bat:

@echo off
set "CallCount=0"
set "Mood="
set /P "Mood=Your mood is: "
call Receive.bat "%Mood%"
rem *Random stuff*
set "Food="
set /P "Food=The food you want is: "
call Receive.bat "%Food%"
set "CallCount="

Receive.bat:

@echo off  
set /A CallCount+=1           
if "CallCount"=="2" goto Call2   
if not "%~1"=="" echo %1     <----
*Random Stuff*                   |
goto :EOF                        |---What is %~1 doing in this area?
:Call2                           |
if not "%~1"=="" echo %1     <----
rem Commands for second call.

Edit: This is a file that uses the call command twice.

like image 960
Kit Avatar asked Feb 19 '15 01:02

Kit


People also ask

What is %~ n0 in batch file?

%0 is the name of the batch file. %~n0 Expands %0 to a file Name without file extension.

What does this mean %~ dp0?

The %~dp0 Variable. The %~dp0 (that's a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file. The variables %0-%9 refer to the command line parameters of the batch file.

What does %% I mean?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt.

What is %1 and %2 in a batch file?

Running shift once in a batch file will make "%1" value to be the second argument, "%2" becomes the third, and so on. It's useful for processing command line arguments in a loop in the batch file. Show activity on this post. %1 is the first argument given, %2 the second.


1 Answers

%1 is the first argument form the invoking command line. If the passed argument has quotes around it, %1 includes the quotes. Where as, %~1 provides the argument value with quotes removed.

Helpful reference here.

like image 135
Chris Noe Avatar answered Sep 30 '22 17:09

Chris Noe