what is %~f0

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hmp_khauff
Posts: 1
Joined: 13 Apr 2009 09:14

what is %~f0

#1 Post by hmp_khauff » 13 Apr 2009 12:33

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

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 13 Apr 2009 13:24

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 14 Apr 2009 00:15

In other words:
%~f0 - resolves to the full name of the currently executing batch file.
:wink:

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 15 Apr 2009 09:40

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.

plp626
Posts: 5
Joined: 17 Apr 2009 00:36
Location: China

#5 Post by plp626 » 17 Apr 2009 01:18

Run this script:

Code: Select all

@echo %%~f0 is %~f0
@pause

Post Reply