Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 453 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is the best way to do a substring in a batch file?

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


Thanks to [this link][1], 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.


[1]:

[To see links please register here]

Reply

#2
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!
Reply

#3
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.
Reply

#4
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!

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through