Page 1 of 1

what is %~f0

Posted: 13 Apr 2009 12:33
by hmp_khauff
i have this instruction in the batch file:
for /f "tokens=1,2,* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo. %%B %%C

i want to know what's the function of this command: %~f0

Posted: 13 Apr 2009 13:24
by RElliott63
The Script must be receiving a path/filename as a parameter.

The "%~f0" takes *all* of the parameters as one and separates the Path of the filename into it's own part.

Posted: 14 Apr 2009 00:15
by DosItHelp
In other words:
%~f0 - resolves to the full name of the currently executing batch file.

Posted: 15 Apr 2009 09:40
by avery_larry
To expand on DosItHelp . . .

call /? will show you what the parameters are that you can use.

%1, %2 you're probably familiar with as the 1st, 2nd parameter that you can pass to a script file. Well, %0 is the actual command that you call.

From the help screen: %~fN (where N is the parameter on the command line you're interested in) "expands %N to a fully qualified path name". This is actually the full path and file name -- regardless of how the file was actually called. Example:

If I'm in the c:\tmp directory and I execute the following command:

Code: Select all

test.cmd c:\windows\file.txt .\file.txt ..\file.txt "c:\program files\file.txt" "a file.txt"

Then the following will be defined:

%~f0 evaluates to c:\tmp\test.cmd
%~f1 evaluates to c:\windows\file.txt
%~f2 evaluates to c:\tmp\file.txt
%~f3 evaluates to c:\file.txt
%~f4 evaluates to c:\program files\file.txt NOTE THERE ARE NO QUOTES
%~f5 evaluates to c:\tmp\a file.txt (still NO QUOTES)

The files don't actually have to exist.

Posted: 17 Apr 2009 01:18
by plp626
Run this script:

Code: Select all

@echo %%~f0 is %~f0