Table of Contents

Powershell

1
$CurrentPath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent

With powershell V3

1
$PSScriptRoot

When converting ps1 to EXE (ps2exe), retrieving the script/exe path can be problematic. The variable that works is”

1
$PWD

vbs

1
2
3
4
5
Function GetPath()
   Dim path
  path = WScript.ScriptFullName
  GetPath = Left(path, InStrRev(path, "\"))
End Function

En batch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
I often use %~dp0 in my scripts
 
echo %~d0
Returns the drive letter (C:).
 
echo %~n0
Returns file name without extension (script).
 
echo %~nx0
Returns file name and extension (script.bat).
 
echo %~x0
Returns the file extension (.bat).
 
echo %~f0
Returns the complete path in long format (C:\Documents and Settings\david\script.bat).
 
echo %~S0
Returns the complete path in long format (C:\Documents and Settings\david\script.bat).
 
echo %~sf0
Returns the complete path in short format  (C:\DOCUME~1\david\script.bat).
 
echo %~p0
Returns the path in long format without the file name or drive letter (\Documents and Settings\david\).
 
echo %~dp0
Returns the path in long format without the file name (C:\Documents and Settings\david\).
 
echo %~sp0
Returns the path in short format without the file name or drive letter (\DOCUME~1\david\).

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.