The problem: where's the script?

Your script or application needs to locate other files or directories relative to its own location.

Finding the "current directory" is easy, but that doesn't help because it isn't necessarily where the script resides.

This situation will often occur when you are interactively using the CMD shell and are running a script in another folder. Another example of how this might happen is running a .CMD script in a network share from explorer. UNC paths for current directories aren't supported by default, so the current directory gets set to %WINDIR%.

CMD Batch file example:

I discovered this techinque in the comments of this post by Raymond Chen: http://blogs.msdn.com/oldnewthing/archive/2005/01/28/362565.aspx

One thing to keep in mind with technique is that %~dp0 includes a trailing slash.

---------------------
set APPDIR=%~dp0
@ECHO OFF

ECHO. 
ECHO Full path to application:
ECHO. 
ECHO ^ %0
ECHO. 

ECHO. 
ECHO Directory:
ECHO. 
ECHO ^ %APPDIR%
ECHO. 

---------------------

 

VBScript Example

The WScript.ScriptFullName contains what we need. All that remains is to strip away the script filename at the end by using the Scripting.FileSystemObject

---------------------


set FSO = CreateObject("Scripting.FileSystemObject") 



wscript.echo "Application Path:"
wscript.echo
wscript.echo " " + Wscript.ScriptFullName

wscript.echo
wscript.echo "Directory:"
wscript.echo
wscript.echo " " + FSO.GetFile(Wscript.ScriptFullName).ParentFolder 


wscript.echo
wscript.echo "Filename:"
wscript.echo
wscript.echo " " + FSO.GetFile(Wscript.ScriptFullName).Name

 


---------------------

 

Python Example

sys.argv[0] contains the full name for the script. Like the VBScript example, we just need to strip away the script filename at the end.

--------------------- 
import os
import sys


if (__name__=='__main__') :

  application_path = sys.argv[0]
    
  print
  print "Full path to application:"
  print " ", application_path
  print
  print "Directory:"
  print " ", os.path.dirname( application_path )
  print
  print "Filename:"
  print " ", os.path.basename( application_path )
--------------------- 

 

C# Example

First, get the currently executing assembly via System.Reflection.Assembly.GetExecutingAssembly().

Then, use that Assembly's Location property.

Finally, strip off the filename to get the path needed.

 

using System;
using System.Collections.Generic;
using System.Text;

namespace csharp_getapppath
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Reflection.Assembly executing_assembly = System.Reflection.Assembly.GetExecutingAssembly();
            string application_path = executing_assembly.Location;
            System.Console.WriteLine();
            System.Console.WriteLine("Full Path to application:");
            System.Console.WriteLine("  \"{0}\"", application_path);
            System.Console.WriteLine();
            System.Console.WriteLine("Directory:");
            System.Console.WriteLine("  \"{0}\"", System.IO.Path.GetDirectoryName(application_path));
            System.Console.WriteLine();
            System.Console.WriteLine("Filename:");
            System.Console.WriteLine("  \"{0}\"", System.IO.Path.GetFileName(application_path));
        }
    }
}

Powershell
function get-script
{
   if($myInvocation.ScriptName) { $myInvocation.ScriptName }
   else { $myInvocation.MyCommand.Definition }
}


$script = get-script
$script_folder = split-path $script

""
"Script Filename" 
$script
""

"Script Folder" 
$script_folder
""