224

I want to get the name of the currently running batch file without the file extension.

Thanks to this link, I have the file name with the extension... but what is the best way to do a substring in a batch file?

Or is there another way to get the file name w/o the extension?

It is safe to assume 3 letter extensions in this scenario.

JosephStyons
  • 57,317
  • 63
  • 160
  • 234

3 Answers3

446

Well, for just getting the filename of your batch the easiest way would be to just use %~n0.

@echo %~n0

will output the name (without the extension) of the currently running batch file (unless executed in a subroutine called by call). The complete list of such “special” substitutions for path names can be found with help for, at the very end of the help:

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

To precisely answer your question, however: Substrings are done using the :~start,length notation:

%var:~10,5%

will extract 5 characters from position 10 in the environment variable %var%.

NOTE: The index of the strings is zero based, so the first character is at position 0, the second at 1, etc.

To get substrings of argument variables such as %0, %1, etc. you have to assign them to a normal environment variable using set first:

:: Does not work:
@echo %1:~10,5

:: Assign argument to local variable first:
set var=%1
@echo %var:~10,5%

The syntax is even more powerful:

  • %var:~-7% extracts the last 7 characters from %var%
  • %var:~0,-4% would extract all characters except the last four which would also rid you of the file extension (assuming three characters after the period [.]).

See help set for details on that syntax.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 36
    Substring extraction doesn't work with FOR loop variables either so you'll have to do as follows (\r\n in the code mean line breaks because SO seem not allowing line breaks in comments): `setlocal ENABLEDELAYEDEXPANSION \r\n for /f %%s in ('set') do ( \r\n set tmp=%%s \r\n echo !tmp:~-3! \r\n )` – Fr0sT Apr 28 '14 at 11:21
  • @Fr0sT why do you have to do this for `FOR` loops? – Bob Jan 18 '17 at 16:09
  • 5
    @Adrian why not? `FOR` is a cornerstone construction in batch files performing pretty huge number of various tasks. Btw, there's one more way to cope with the task by using a subroutine, i.e. `for /f %%s in ('set') do call :DoSmth "%%s" \r\n ... \r\n :DoSmth \r\n set var=%1 \r\n echo %var:~1,3%` – Fr0sT Jan 19 '17 at 07:17
  • 1
    @Phate01, don't use `%date%` to construct a date in a certain format. The format is specific to the user's culture and may change. Use WMI instead to get date and time in a useful format that doesn't depend on the user's settings. Also I'm not sure what this comment is doing here, as this question was not about date or time. – Joey May 22 '18 at 07:32
  • 1
    Actually you're right as I guess I wanted to comment a different question :) – Phate01 May 22 '18 at 07:57
  • **`%var is base 0`** this is implicitly only :( derived from the second sample. – user6698332 May 23 '18 at 11:26
  • @Fr0sT It does work, yes, but you have to use `setlocal ` and enable delayed expansion. – user2262111 Sep 23 '18 at 10:04
  • @user2262111 isn't that what I said in comment from Apr 28 '14 ? – Fr0sT Sep 24 '18 at 07:07
  • How to use `~othervar` in place of `~10`? It doesn't work for me. – johny why Nov 20 '21 at 06:02
42

Nicely explained above!

For all those who may suffer like me to get this working in a localized Windows (mine is XP in Slovak), you may try to replace the % with a !

So:

SET TEXT=Hello World
SET SUBSTRING=!TEXT:~3,5!
ECHO !SUBSTRING!
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
BrandonSk
  • 429
  • 4
  • 2
13

As an additional info to Joey's answer, which isn't described in the help of set /? nor for /?.

%~0 expands to the name of the own batch, exactly as it was typed.
So if you start your batch it will be expanded as

%~0   - mYbAtCh
%~n0  - mybatch
%~nx0 - mybatch.bat

But there is one exception, expanding in a subroutine could fail

echo main- %~0
call :myFunction
exit /b

:myFunction
echo func - %~0
echo func - %~n0
exit /b

This results to

main - myBatch
Func - :myFunction
func - mybatch

In a function %~0 expands always to the name of the function, not of the batch file.
But if you use at least one modifier it will show the filename again!

jeb
  • 78,592
  • 17
  • 171
  • 225