Path

Path

Source:

Adds functions (similar to Node.js path) that handles file path strings into WSH.

Requires

Members

(static, constant) delimiter :string

Source:

Windows path delimiter.

Type:
  • string
Example
var path = Wsh.Path; // Shorthand

console.log(path.delimiter); // ;

(static, constant) sep :string

Source:

Windows path segment separator.

Type:
  • string
Example
var path = Wsh.Path; // Shorthand

console.log(path.sep); // \

Methods

(static) basename(str, extopt) → {string}

Source:

Returns the last portion of the path all given path segments. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.basename('C:\\foo\\bar\\baz\\quux.html');
// Returns: 'quux.html'

path.basename('C:\\foo\\bar\\baz\\quux.html', '.html');
// Returns: 'quux'

path.basename('C:\\my-repo\\.gitignore', '.gitignore');
// Returns: '.gitignore'

path.basename('C:\\My Data');
// Returns: 'My Data'

// UNC
path.basename('\\\\CompName\\SharedDir\\photo.jpg');
// Returns: 'photo.jpg'

path.basename('\\\\CompName\\SharedDir');
// Returns: 'SharedDir'

path.basename('\\\\CompName\\');
// Returns: 'CompName'
Parameters:
Name Type Attributes Description
str string

The path to convert.

ext string <optional>

An optional file extension.

Returns:
  • The last position of the path.
Type
string

(static) dirname(str) → {string}

Source:

Returns the directory name of the path. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.dirname('C:\\My Data\\image.jpg');
// Returns: 'C:\\My Data'

path.dirname('C:\\My Data');
// Returns: 'C:\\'

path.dirname('C:\\');
// Returns: 'C:\\'

path.dirname('D:\\logs\\*.txt');
// Returns: 'D:\\logs'

// UNC
path.dirname('\\\\CompName\\SharedDir\\photo.jpg');
// Returns: '\\\\CompName\\SharedDir\\'

path.dirname('\\\\CompName\\SharedDir');
// Returns: '\\\\CompName\\SharedDir'

// Unix-like (POSIX)
path.dirname('C:/foo/bar/baz/asdf/quux');
// Returns: 'C:/foo/bar/baz/asdf'
Parameters:
Name Type Description
str string

The path to convert.

Returns:
  • The directory name of path.
Type
string

(static) extname(str) → {string}

Source:

Returns the extension of the path. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.extname('index.html');
// Returns: '.html'

path.extname('index.coffee.md');
// Returns: '.md'

path.extname('.gitignore');
// Returns: ''

path.extname('C:\\foo\\bar\\baz\\quux.html');
// Returns: '.html'

// UNC
path.extname('\\\\CompName\\SharedDir\\photo.jpg');
// Returns: '.jpg'

path.extname('\\\\CompName\\SharedDir');
// Returns: ''
Parameters:
Name Type Description
str string

The path to convert.

Returns:
  • the extension of the path.
Type
string

(static) isAbsolute(str) → {boolean}

Source:

Determines if path is an absolute path. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.isAbsolute('C:\\My Data\\hoge.png'); // true
path.isAbsolute('C:\\My Data\\..'); // true
path.isAbsolute('bar\\baz'); // false
path.isAbsolute('.'); // false
// UNC
path.isAbsolute('\\\\CompName'); // true
// Unix-like (POSIX)
path.isAbsolute('C:/My Data/hoge.png'); // true
path.isAbsolute('C:/My Data/..'); // true
path.isAbsolute('//CompName'); // true
path.isAbsolute('bar/baz'); // false
Parameters:
Name Type Description
str string

The path to check.

Returns:
  • Returns true if the string is an absolute, else false.
Type
boolean

(static) isUNC(str) → {boolean}

Source:

Checks if the string is UNC (Universal Naming Convention).

Example
var path = Wsh.Path;

path.isUNC('\\\\CompName'); // false
path.isUNC('\\\\CompName\\'); // false
path.isUNC('\\\\CompName\\SharedDir'); // true

// Long UNC
path.isUNC('\\\\?\\UNC\\CompName\\SharedDir'); // true

// LFS (Local File System)
path.isUNC('C:\\foo\\bar.baz'); // false

// Unix-like (POSIX)
path.isUNC('//CompName//SharedDir'); // false
Parameters:
Name Type Description
str string

The string to check.

Returns:
  • Returns true if the string is an UNC, else false.
Type
boolean

(static) join(…arguments) → {string}

Source:

Joins all given path segments. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.join('C:', 'Program Files (x86)', 'Microsoft.NET', 'RedistList', 'AssemblyList_4_client.xml');
// Returns: 'C:\\Program Files (x86)\\Microsoft.NET\\RedistList\\AssemblyList_4_client.xml'

path.join('C:\\Git\\', '\\mingw64\\', 'lib', '..\\etc', '.gitconfig');
// Returns: 'C:\\Git\\mingw64\\etc\\.gitconfig''

var argVar = ['mingw64\\lib', '..\\etc', '.gitconfig'];
path.join.apply(null, argVar);
// Returns: 'mingw64\\etc\\.gitconfig'

path.join('\\\\CompName', 'Public', 'photo3.jpg');
// Returns: '\\\\CompName\\Public\\photo3.jpg'
Parameters:
Name Type Attributes Description
arguments string <repeatable>

Parts of the file path.

Returns:
  • The Joined patha
Type
string

(static) normalize(str) → {string}

Source:
To Do:
  • '~' to '%USERPROFILE%'

Resolve '..' and '.' segments. Replace path segment separation characters. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// Returns: 'C:\\temp\\foo\\'

path.normalize('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:\\temp\\foo\\bar'

// bash.exe は/C/~がC:\を意味するが、それは解釈しない
path.normalize('C/Git/mingw64/bin/git.exe');
// Returns: 'C\\Git\\mingw64\\bin\\git.exe'

path.normalize('C:\\Git\\mingw64\\lib\\..\\etc\\.gitconfig');
// Returns: 'C:\\Git\\mingw64\\etc\\.gitconfig'

path.normalize('..\\.config\\settings.json');
// Returns: '..\\.config\\settings.json'

path.normalize('settings.json');
// Returns: 'settings.json'
Parameters:
Name Type Description
str string

The path to convert.

Returns:
  • The formatted path.
Type
string

(static) parse(str) → {object}

Source:

Returns an object whose properties. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.parse('C:\\home\\user\\dir\\file.txt');
// Returns:
// { root: 'C:\\',
//   dir: 'C:\\home\\user\\dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' };

path.parse('\\\\CompName\\Public\\photo3.jpg');
// Returns:
// { root: '\\\\CompName\\Public\\',
//   dir: '\\\\CompName\\Public\\',
//   base: 'photo3.jpg',
//   ext: '.jpg',
//   name: 'photo3' };

path.parse('C:\\.git');
// Returns:
// { root: 'C:\\',
//   dir: 'C:\\',
//   base: '.git',
//   ext: '',
//   name: '.git' };

// Unix-like (POSIX)
path.parse('C:/home/user/dir/file.txt');
// Returns:
// { root: 'C:/',
//   dir: 'C:/home/user/dir',
//   base: 'file.txt',
//   ext: '.txt',
//   name: 'file' };
Parameters:
Name Type Description
str string

The path to parse.

Returns:
  • The parsed object.
Type
object

(static) relative(fromDir, toPath) → {string}

Source:

Returns the relative path from fromDir to toPath. Similar to Node.js Path.

Example
var path = Wsh.Path;
var pathA, pathB;

pathA = 'C:\\MyApps\\Gimp';
pathB = 'D:\\Apps\\ImageMagick\\convert.exe';
path.relative(pathA, pathB);
// Returns: 'D:\\Apps\\ImageMagick\\convert.exe'

pathA = 'C:\\MyApps';
pathB = 'C:\\MyApps\\convert.exe';
path.relative(pathA, pathB);
// Returns: 'convert.exe'

pathA = 'C:\\MyApps\\Paint\\Gimp';
pathB = 'C:\\MyApps\\Converter\\ImageMagick\\convert.exe';
path.relative(pathA, pathB);
// Returns: '..\\..\\Converter\\ImageMagick\\convert.exe'

pathA = 'C:\\MyApps\\Gimp';
pathB = 'C:\\ImageMagick\\convert.exe';
path.relative(pathA, pathB);
// Returns: '..\\..\\ImageMagick\\convert.exe'

pathA = 'C:\\MyApps\\Paint\\Gimp';
pathB = 'C:\\convert.exe';
path.relative(pathA, pathB);
// Returns: '..\\..\\..\\convert.exe'
Parameters:
Name Type Description
fromDir string

The start directory path.

toPath string

The end path.

Returns:
  • The relative path.
Type
string

(static) resolve(…arguments) → {string}

Source:

Resolves the sequence of paths or path segments into an absolute path. Similar to Node.js Path.

Example
var path = Wsh.Path;

path.resolve('');
// Returns: <The current direcotry path>

path.resolve('C:', 'Program Files (x86)', 'Microsoft.NET', 'RedistList', 'AssemblyList_4_client.xml');
// Returns: 'C:\\Program Files (x86)\\Microsoft.NET\\RedistList\\AssemblyList_4_client.xml'

path.resolve('C:\\Git\\', '\\mingw64\\', 'lib', '..\\etc', '.gitconfig');
// Returns: 'C:\\Git\\mingw64\\etc\\.gitconfig'

path.resolve('mingw64\\lib', '..\\etc', '.gitconfig');
// Returns: '<Current Path>\\mingw64\\etc\\.gitconfig'

path.resolve('\\\\CompName', 'Public', 'photo3.jpg');
// Returns: '\\\\CompName\\Public\\photo3.jpg'

var argVar = ['C:\\Git', 'D:\\bin\\git\\', '\\etc', '.gitconfig'];
path.resolve.apply(null, argVar);
// Returns: 'D:\\bin\\git\\etc\\.gitconfig'
Parameters:
Name Type Attributes Description
arguments string <repeatable>

Parts of the file path.

Returns:
  • The resolved path.
Type
string

(static) toUNC(str) → {string}

Source:

Convert the LFS (Local File System) path to UNC.

Example
var path = Wsh.Path;

path.toUNC('//CompName');
// Returns: '//CompName'

path.toUNC('C:\\foo\\bar.baz');
// Returns: '\\\\<Your CompName>\\C$\\foo\\bar.baz'

// Unix-like (POSIX)
path.toUNC('C:/foo/bar.baz');
// Returns: '\\\\<Your CompName>\\C$\\foo\\bar.baz'

path.toUNC('');
// Returns: ''
Parameters:
Name Type Description
str string

The path to convert.

Returns:
  • The UNC path.
Type
string