What is the best way to do a substring in a batch file?

Batch FileSubstring

Batch File Problem Overview


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.

Batch File Solutions


Solution 1 - Batch File

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.

Solution 2 - Batch File

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!

Solution 3 - Batch File

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosephStyonsView Question on Stackoverflow
Solution 1 - Batch FileJoeyView Answer on Stackoverflow
Solution 2 - Batch FileBrandonSkView Answer on Stackoverflow
Solution 3 - Batch FilejebView Answer on Stackoverflow