FileSystem

FileSystem

Source:

This module takes charge of handling file and directory (similar to Node.js-FileSystem).

Requires

Namespaces

constants

Methods

(static) copyFileSync(src, dest, flagopt) → {void}

Source:

Synchronously copies src to dest. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

fs.copyFileSync('D:\\SrcFile.path', 'R:\\DestFile.path');

// The following does not work.
// Ex.1 Directory
fs.copyFileSync('D:\\SrcDir', 'R:\\DestDir');
// Ex.2 Non-existing parent directory
fse.copySync('D:\\SrcFile.path', 'R:\\NonExistingDir\\DestFile.path');
Parameters:
Name Type Attributes Description
src string

The source file path.

dest string

The destination file path.

flag Wsh.FileSystem.constants.COPYFILE_EXCL <optional>
Returns:
Type
void

(static) excludeSymboliclinkPaths(filePaths, optionsopt) → {Array.<string>}

Source:

Excludes Symbolic-link paths from the list of file-path.

Example
var fs = Wsh.FileSystem; // Shorthand
var list = [
  'D:\\My Dir\\memo.txt',
  'D:\\MyBook-SymLink.xlsx', // SymbolicLink
  'D:\\image.png' ];

var files = fs.excludeSymboliclinkPaths(list);
// Returns: [
//   'D:\\My Dir\\memo.txt',
//   'D:\\image.png' ];
Parameters:
Name Type Attributes Description
filePaths Array.<string>

The list of file paths to check.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
ignoresErr boolean <optional>
false

Even if an error occurs during processing, it will be ignored.

Returns:
  • The list of file paths that excluded symlinks.
Type
Array.<string>

(static) existsSync(fpath) → {boolean}

Source:

Returns true if the path exists, false otherwise. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

fs.existsSync('D:\\ExistingDir'); // true
fs.existsSync('D:\\ExistingDir\\File.path'); // true
fs.existsSync('D:\\NonExistingDir\\File.path'); // false
fs.existsSync('\\\\MyComp\\Public\\ExistingDir\\File.path'); // true
Parameters:
Name Type Description
fpath string

The file-path to check.

Returns:
  • If the file is existing returns true. else false.
Type
boolean

(static) getAllChildrensFullPaths(dirPath, optionsopt) → {Array.<string>}

Source:

[Experimental] Creates a list of files in the directory with using dir instead of fso.GetFolder.

Parameters:
Name Type Attributes Description
dirPath string

The directory path to list.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
ignoresErr boolean <optional>
false

Even if an error occurs during processing, it will be ignored.

isOnlyDir boolean <optional>
false
excludesSymlink boolean <optional>
false
Returns:
  • The list of full file paths.
Type
Array.<string>

(static) getChildrenDirectories(dirPath, optionsopt) → {Array.<string>|Array.<Object>}

Source:

Gets directories info in the specified directory.

Parameters:
Name Type Attributes Description
dirPath string

A directory path to read.

options typeGetChildrenFilesOptions <optional>

Optional parameters.

Returns:
Type
Array.<string> | Array.<Object>

(static) getChildrenFiles(dirPath, optionsopt) → {Array.<string>|Array.<Object>}

Source:

Gets files info in the specified directory.

Parameters:
Name Type Attributes Description
dirPath string

The directory path to read.

options typeGetChildrenFilesOptions <optional>

Optional parameters.

Returns:
Type
Array.<string> | Array.<Object>

(static) inspectPathWhetherMAX_PATH(fullPath) → {void}

Source:

Checks the length of a path. In the Windows API, the maximum length for a path is MAX_PATH, which is defined as 260 characters. Maximum Path Length Limitation.

Example
var tooLongPath = 'C:\\Users\\UserName\\AppData\\Roaming\\Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\ImplicitAppShortcuts\\database\\Document and Settings\\MongoDB Inc\\MongoDB Compass Community\\computer\\operationsManagement\\img\\01_original_screencapture-20180213T043515+0900.png';

Wsh.FileSystem.inspectPathWhetherMAX_PATH(tooLongPath); // Throws an Error
Parameters:
Name Type Description
fullPath string

The file-path to chekc.

Throws:
  • If a length of the file-path is over then 255.
Type
string
Returns:
Type
void

(static) linkSync(existingPath, newPath, optionsopt) → {boolean|string}

Source:

[Requires admin rights] Creates a new link (also known as Symbolic Link) to an existing file. Similar to Node.js-Path

Example
// Run this script as admin
var fs = Wsh.FileSystem; // Shorthand

fs.linkSync('D:\\MyDir\\BackUp', 'C:\\BackUp-Symlink'); // Requires admin
Parameters:
Name Type Attributes Description
existingPath string
newPath string
options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
msecTimeOut number <optional>
10000

default: 10sec

isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • If isDryRun is true, returns string.
Type
boolean | string

(static) mkdirSync(dirPath, optionsopt) → {void}

Source:

Synchronously creates the directory. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

fs.mkdirSync('D:\\MyDir');

// The following does not work.
// Ex.1 The existing directory
fs.mkdirSync('D:\\MyDir'); // Throws an Error
// Ex.2 Non-existing parent directory
fs.mkdirSync('R:\\NonExistingDir\\NewDir'); // Throws an Error
Parameters:
Name Type Attributes Description
dirPath string

The directory file path to create.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Description
isRecursive boolean <optional>

@TODO W.I.P

Returns:
Type
void

(static) readdirSync(dirPath, optionsopt) → {Array.<string>|Array.<Object>}

Source:

Reads the contents of a directory. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

var testDir = 'D:\\testDir';
//  D:\testDir\
//  │  fileRoot1.txt
//  │  fileRoot2-Symlink.log // <SYMLINKD>
//  │  fileRoot2.log
//  │
//  ├─DirBar\
//  ├─DirBar-Symlink\ // <SYMLINKD>
//  └─DirFoo\
//          fileFoo1.txt

// Ex.1 No options
fs.readdirSync(testDir);
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2-Symlink.log', // <SYMLINKD>
//   'fileRoot2.log',
//   'DirBar',
//   'DirBar-Symlink', // <SYMLINKD>
//   'DirFoo' ]

// Ex.2 Files only
fs.readdirSync(testDir, { isOnlyFile: true });
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2-Symlink.log', // <SYMLINKD>
//   'fileRoot2.log' ]

// Ex.3 Directories only
fs.readdirSync(testDir, { isOnlyDir: true });
// Returns: [
//   'DirBar',
//   'DirBar-Symlink', // <SYMLINKD>
//   'DirFoo' ]

// Ex.4 Excludes Symlink
fs.readdirSync(testDir, { excludesSymlink: true });
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2.log',
//   'DirBar',
//   'DirFoo' ]

// Ex.5 Filtering
fs.readdirSync(testDir, { matchedRegExp: '\\d+\\.txt$' });
// Returns: ['fileRoot1.txt']

// Ex.6 withFileTypes
fs.readdirSync(testDir, { withFileTypes: true });
// Returns: [
//   { name: 'fileRoot1.txt',
//     path: 'D:\\testDir\\fileRoot1.txt',
//     attributes: 32,
//     isDirectory: false,
//     isFile: true,
//     isSymbolicLink: false },
//   ...
//   ..
//   { name: 'DirFoo.txt',
//     path: 'D:\\testDir\\DirFoo',
//     attributes: 16,
//     isDirectory: true,
//     isFile: false,
//     isSymbolicLink: false }]
Parameters:
Name Type Attributes Description
dirPath string

The directory path to read.

options typeReaddirSyncOptions <optional>

Optional parameters.

Returns:
Type
Array.<string> | Array.<Object>

(static) readFileSync(fpath, optionsopt) → {unknown|string}

Source:

Synchronously reads the entire contents of a file. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

var readBin = fs.readFileSync('D:\\MyPage\\index.html');
// The default is reading the file as binary

var readHtml = fs.readFileSync('D:\\MyPage\\index.html', { encoding: 'utf8' });
var readText = fs.readFileSync('D:\\MyNote\\memo.txt', { encoding: 'sjis' });
Parameters:
Name Type Attributes Description
fpath string

The file-path to read.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
encoding string <optional>
'binary'

latin1 (iso-8859-1), utf8, utf16, sjis (shift_jis, cp932), All Charset -> HKEY_CLASSES_ROOT\Mime\Database\Charset\

throws boolean <optional>
true

Whether throws an error or not when catch.

Returns:
  • 'unknown' is the result of the typeof judgment of JScript. Means binary?
Type
unknown | string

(static) rmdirSync(dirPath) → {void}

Source:

Removes the directory. Can not remove a file. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

fs.rmdirSync('D:\\MyDir');

// The following does not work.
// Ex.1 File
fs.rmdirSync('D:\\MyFile.path');
// Ex.2 A non-existing directory
fs.mkdirSync('R:\\NonExistingDir');
Parameters:
Name Type Description
dirPath string

The directory path to delete.

Returns:
Type
void

(static) statSync(fpath) → {typeFsStat}

Source:

Returns information about the file. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

var stat = fs.statSync('D:\\My Dir\\File.path');
stat.size; // 2345
stat.isFile(); // true
stat.isDirectory(); // false
stat.isSymbolicLink(); // false
stat.atime; // Thu Sep 3 07:16:02 UTC+0900 2020,
stat.mtime; // Sat Feb 3 07:18:51 UTC+0900 2018,
stat.ctime; // null,
stat.birthtime; // Thu Sep 3 07:16:02 UTC+0900 2020

var stat = fs.statSync('D:\\Symlink Dir');
stat.size; // 0
stat.isFile(); // false
stat.isDirectory(); // true
stat.isSymbolicLink(); // true
Parameters:
Name Type Description
fpath string

The file-path to check.

Returns:
  • An object provides information about a file.
Type
typeFsStat

(static) unlinkSync(fpath) → {void}

Source:

Removes the file with DeleteFile method. Can not remove a directory. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand

fs.unlinkSync('D:\\MyFile.path');
fs.unlinkSync('D:\\MyDir'); // Throws an Error
Parameters:
Name Type Description
fpath string

The file-path to remove.

Returns:
Type
void

(static) writeFileSync(fpath, data, optionsopt) → {void}

Source:

Synchronously writes data to the file. Similar to Node.js-Path

Example
var fs = Wsh.FileSystem; // Shorthand
var writeData = 'Foo Bar';

// The default is writing data to the file as binary (UTF-16 LE NoBOM)
fs.writeFileSync('D:\\MyData.bin', writeData);

fs.writeFileSync('D:\\my-note.txt', writeData, { encoding: 'utf8' });

fs.writeFileSync('D:\\MyNote.txt', writeData, { encoding: 'sjis' });

fs.writeFileSync('D:\\my-script.wsf', writeData, {
  encoding: 'utf8', bom: true
});

fs.writeFileSync('D:\\fixme.txt', '', { encoding: 'utf8' });
// Thorws a error! Fix this (T_T)
Parameters:
Name Type Attributes Description
fpath string

The file-path to write.

data string | Buffer

Data of the file.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
encoding string <optional>
'binary'

utf8(UTF-8), utf16(UTF-16 LE), sjis(Shift_JIS, CP932), All Charset -> HKEY_CLASSES_ROOT\Mime\Database\Charset\

bom boolean <optional>
false

true => enable, others => disable

Returns:
Type
void

(static) writeTmpFileSync(data, optionsopt) → {string}

Source:

Writes the data to a new temporary path, and Return the path.

Example
var fs = Wsh.FileSystem; // Shorthand
var writeData = 'Foo Bar';

var tmpPath = fs.writeTmpFileSync(writeData, { encoding: 'utf8' });
// Returns: 'C:\\Users\\UserName\\AppData\\Local\\Temp\\fs-writeTmpFileSync_rad6E884.tmp'
Parameters:
Name Type Attributes Description
data string

The temporary data to write.

options object <optional>

See Wsh.FileSystem.writeFileSync

Returns:
  • The temporary file path.
Type
string

(static) xcopySync(src, dest, optionsopt) → {void|typeExecSyncReturn|string}

Source:

Copies file with xcopy.

Example
var fs = Wsh.FileSystem; // Shorthand

fs.xcopySync('D:\\SrcFile.path', 'R:\\DestFile.path');
fs.xcopySync('D:\\SrcDir', 'R:\\DestDir');

var stdObj = fs.xcopySync('D:\\src.js', 'R:\\dest.js', { withStd: true });
// Returns: {
//   error: false,
//   exitCode: 0,
//   stdout: '<String of StdOut>',
//   stderr: '<String of StdErr>' }
Parameters:
Name Type Attributes Description
src string

The source file/directory path.

dest string

The destination file/directory path.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
withStd boolean <optional>
false
isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
Type
void | typeExecSyncReturn | string