Is there a variable that represents a script's file name (excluding extensions)?

2

I am making a batch program that will create a .ini settings file after execution. I want the name of the settings file (excluding the extension) to be drawn from the scripts file name, so if it is changed, the settings name will change to mirror it upon it's creation. I have used programs like HxD that can do this, but I have no idea how. Is there a variable, or wildcard that can be used to do this?

I have analyzed some programs that do this, but some seem to use different values/methods. One of these programs used %d.ini.

Mr. Mendelli

Posted 2017-07-27T10:41:57.107

Reputation: 1 061

Answers

2

I want the name of the settings file to be the script filename

Use the following expression to get the script file name:

%~n0

Example usage:

> dir test.cmd
 Volume in drive F is Expansion
 Volume Serial Number is 3656-BB63

 Directory of F:\test

27/07/2017  11:47                65 test.cmd
               1 File(s)             65 bytes
               0 Dir(s)  1,627,427,311,616 bytes free

> type test.cmd
@echo off
setlocal enabledelayedexpansion
echo %~n0
endlocal

> test
test

Note:

  • %0 is the name of the batch file.
  • %~n0 Expands %0 to a file Name without file extension

Further Reading

DavidPostill

Posted 2017-07-27T10:41:57.107

Reputation: 118 938

Would this possibly work if I convert the script to an EXE binary? I am debating doing this after compiling everything? – Mr. Mendelli – 2017-07-27T10:53:19.587

@Mr.Mendelli No idea. Try it and see. – DavidPostill – 2017-07-27T10:54:15.050

That did it! Thank you for your answer, and yes, it worked after the conversion. – Mr. Mendelli – 2017-07-27T10:57:32.443