How Might I Get the Script Filename from Within That Script

How might I get the script filename from within that script?

var scripts = document.getElementsByTagName('script');
var lastScript = scripts[scripts.length-1];
var scriptName = lastScript.src;
alert("loading: " + scriptName);

Tested in: FF 3.0.8, Chrome 1.0.154.53, IE6



See also: How may I reference the script tag that loaded the currently-executing script?

How do I know the script file name in a Bash script?

me=`basename "$0"`

For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try:

me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"

IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!? Must be a bug!" Besides, one of the purposes of having differently-named symlinks is to provide different functionality based on the name it's called as (think gzip and gunzip on some platforms).


1 That is, to resolve symlinks such that when the user executes foo.sh which is actually a symlink to bar.sh, you wish to use the resolved name bar.sh rather than foo.sh.

Can javascript file get its own name?

This should work:
(new Error).fileName

Or you can try this:

var filepath;
(function(){
var scripts = document.getElementsByTagName('script');
filepath = scripts[ scripts.length-1 ].src;
}());

The second option gives you the path of your script file.

Get the current script file name

Just use the PHP magic constant __FILE__ to get the current filename.

But it seems you want the part without .php. So...

basename(__FILE__, '.php'); 

A more generic file extension remover would look like this...

function chopExtension($filename) {
return pathinfo($filename, PATHINFO_FILENAME);
}

var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"

Using standard string library functions is much quicker, as you'd expect.

function chopExtension($filename) {
return substr($filename, 0, strrpos($filename, '.'));
}

Get name of current script in Python

You can use __file__ to get the name of the current file. When used in the main module, this is the name of the script that was originally invoked.

If you want to omit the directory part (which might be present), you can use os.path.basename(__file__).

How do I retrieve the filename of the current script in AutoHotkey?

Use: A_ScriptName The filename of the current script, without its path, e.g. MyScript.ahk.

MsgBox, The current script is %A_ScriptName%

See: https://autohotkey.com/docs/Variables.htm#BuiltIn

and more particularly, https://autohotkey.com/docs/Variables.htm#ScriptName

How to get filename of script being executed in NodeJS?

You can use variable __filename

http://nodejs.org/docs/latest/api/globals.html#globals_filename

How can I get the name of the script calling the function in python?

You could print sys.argv[0] to get the script filename.

To get the filename of the caller, you need to use the sys._getframe() function to get the calling frame, then you can retrieve the filename from that:

import inspect, sys

print inspect.getsourcefile(sys, sys._getframe(1))

python how to get a running script's file name in import package

You should be able to use arguments to work it out.

from sys import argv
print(argv[0])

The first argument listed will be the command used to execute your script.
So if you're running ./main.py then that's what you'll get.

If you're running it via python (such as python main.py) then (at least according to my testing) you'll get the full path. You can use the tools in os.path to pluck out just the filename if required.



Related Topics



Leave a reply



Submit