FileSystemExtra

FileSystemExtra

Source:

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

Requires

Methods

(static) calcCryptHash(filepath, algorithmopt, optionsopt) → {string}

Source:

Generates the cryptographic hash from the file. If the file size is 0, returns 0.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

fse.calcCryptHash('D:\File.txt'); // Default: SHA256
// Returns:
// 1053ed4aca3f61644f2aeb9be175480321530656653853f10b660652777955dd

fse.calcCryptHash('D:\File.txt', 'SHA256');
// Returns:
// 1053ed4aca3f61644f2aeb9be175480321530656653853f10b660652777955dd

fse.calcCryptHash('D:\File.txt', 'MD5');
// Returns:
// 51d52911dc0b646cfda6bb6a6ffa7525

fse.calcCryptHash('D:\.gitkeep', 'MD5'); // The file size is zero
// Returns: 0
Parameters:
Name Type Attributes Default Description
filepath string

The file path to check.

algorithm string <optional>
SHA256

MD2, MD4, MD5, SHA1, SHA256, SHA384, SHA512

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
fsStatObj object <optional>

The object of fs.statSync returned.

isDryRun boolean <optional>
false

No execute, returns the string of command.

Returns:
  • The cryptographic hash or 0. if isDryRun is true, returns the command log string. Not execute.
Type
string

(static) compareFilesOfModifiedDate(fpA, fpB, optionsopt) → {boolean}

Source:

Compares two files by modification date.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

fse.compareFilesOfModifiedDate('C:\FileA.txt', 'D:\FileB.txt');
// Returns: true or false
Parameters:
Name Type Attributes Description
fpA string

The file path to compare.

fpB string

Another file path.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Description
fsAStatObj object <optional>

The fs.stat object of fpA.

fsBStatObj object <optional>

The fs.stat object of fpB.

Returns:
  • If the same date, return true.
Type
boolean

(static) copySync(src, dest, optionsopt) → {void}

Source:

Copies the file or directory. Similar to npm jprichardson/fs-extra

Example
var fse = Wsh.FileSystemExtra; // Shorthand

// File
fse.copySync('D:\\SrcFile.path', 'R:\\DestFile.path');

// Auto creating directory
fse.copySync('D:\\SrcFile.path', 'R:\\NonExistingDir\\DestFile.path');

// Directory
var src = 'D:\\SrcDir';
var dest = 'R:\\DestDir';
fse.copySync(src, dest);
// Note: Copy everything inside of this directory,
//   not the entire directory itself.
// If you want to copy even the directory itself do the following
var path = Wsh.Path;
fse.copySync(src, path.join(dest, path.basename(src)));
Parameters:
Name Type Attributes Description
src string

Note that if src is a directory it will copy everything inside of this directory, not the entire directory itself.

dest string
options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
overwrite boolean <optional>
true

Overwrite existing file or directory.

errorOnExist boolean <optional>
false

When overwrite is false and the destination exists, throw an error.

filter function <optional>

Return true to copy, false to not.

Returns:
Type
void

(static) copyWshFilesDefinedInWsf(srcWsf, destDir) → {void}

Source:

[Experimental] Copies script files defined in the .wsf file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

fse.copyWshFilesDefinedInWsf('D:\\src.wsf','D:\\destDir');
Parameters:
Name Type Description
srcWsf string

The .wsf file.

destDir string

The destination directory path.

Returns:
Type
void

(static) dirTree(dirPath, optionsopt) → {object}

Source:

Can not work. Please use Wsh.FileSystemExtra.readdirSyncRecursively.

Parameters:
Name Type Attributes Description
dirPath string

A directory path

options object <optional>
Properties
Name Type Attributes Default Description
isDirOnly boolean <optional>
false
extensions string <optional>

Ex. {extensions:'.txt|.doc'} 正規表現。ファイルの場合のみ有効なオプション

exclude string <optional>

Ex. {exclude:'C:\tkn\tmp'} 正規表現。フルパスで判定される除外オプション

Returns:
Type
object

(static) ensureDirSync(dirPath) → {void}

Source:

Ensures that the directory exists. If the directory structure does not exist, it is created. Similar to npm jprichardson/fs-extra

Example
var fse = Wsh.FileSystemExtra; // Shorthand

fse.ensureDirSync('D:\\MyDir');
fse.ensureDirSync('D:\\MyDir'); // No Error
fse.ensureDirSync('R:\\NonExistingDir\\NewDir'); // Creates 2 directories
Parameters:
Name Type Description
dirPath string

The directory path to check and create.

Returns:
Type
void

(static) ensureReadingFile(fpath, msecTimeOutopt, optionsopt) → {number}

Source:

Reads the file with Wsh.FileSystem.readFileSync. If the reading fails, it will retry to read for the specified number of seconds.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

var readBin = fse.ensureReadingFile('D:\\logger.log', 5000);
// Retry for 5sec
Parameters:
Name Type Attributes Default Description
fpath string

The file path to read.

msecTimeOut number <optional>
10000

default: 10sec. 0 to not timeout.

options object <optional>

See options.

Returns:
Type
number

(static) ensureRemovingFile(fpath, msecTimeOutopt) → {boolean}

Source:

Ensure to delete a file. 失敗してもリトライし時間経過で諦める

Parameters:
Name Type Attributes Default Description
fpath string
msecTimeOut number <optional>
30000

default: 30sec

Returns:
Type
boolean

(static) findRequiredFile(pathStr, optionsopt) → {string}

Source:

[Experimental] Gets a full file path from the argument string.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

var setPath = fse.findRequiredFile('./settings.json');
// Searches for existing files with the following priority
// <Current Dir>\\settings.json
// <UserProfile Dir>\\settings.json
Parameters:
Name Type Attributes Description
pathStr string
options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
ext string <optional>
''

Ex. ".js" (Don't foget ".")

Returns:
Type
string

(static) getTruePath(fp) → {string}

Source:

[W.I.P] Gets the true path of the symbolic link.

Parameters:
Name Type Description
fp string

Filepath

Returns:
Type
string

(static) isTheSameFile(fpA, fpB, algorithmopt) → {boolean}

Source:

Compares two files by modification date or hash value.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

// Default: cpmpare by modification date
fse.isTheSameFile('C:\FileA.txt', 'D:\FileB.txt');
// Returns: true or false

fse.isTheSameFile('C:\FileA.txt', 'D:\FileB.txt', 'MD5');
// Returns: true or false
Parameters:
Name Type Attributes Default Description
fpA string

A file path

fpB string

Another file path

algorithm string <optional>
'date'

'date'(Modification), 'MD2', 'MD4', 'MD5', 'SHA1', 'SHA256', 'SHA384', 'SHA512'

Returns:
Type
boolean

(static) readCsvFileAsAssocArray(csvpath, optionsopt) → {Array}

Source:

Gets an array of objects from the CSV file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand
var csvFile = 'D:\\MyData.csv';
// The content of this csv is
// ```
// This CSV was output from Tuckn Hoge system
// A,B,C,D,E,F,G,H,I,J,K,L
// 0,1,2,3,4,5,6,7,8,9,10,11
// a,b,c,d,e,f,g,h,i,j,k,l
//
// true,false,null,undefined,NaN,Infinity
// =SUM(X1:Y10),=TODAY(),2020/1/1,'007,Has Space,日本語,I say "Yes!","Line
// Break"
// ```

var readObj = readCsvFileAsAssocArray(csvFile, { beginRow: 2 });
// line 1 will be ignored, line 2 to be header (property) name.
// Returns: [
//   {
//     A: '0',
//     B: '1',
//     C: '2',
//     D: '3',
//     E: '4',
//     F: '5',
//     G: '6',
//     H: '7',
//     I: '8',
//     J: '9',
//     K: '10',
//     L: '11'
//   },
//   {
//     A: 'a',
//     B: 'b',
//     C: 'c',
//     D: 'd',
//     E: 'e',
//     F: 'f',
//     G: 'g',
//     H: 'h',
//     I: 'i',
//     J: 'j',
//     K: 'k',
//     L: 'l'
//   },
//   {
//     A: 'true',
//     B: 'false',
//     C: 'null',
//     D: 'undefined',
//     E: 'NaN',
//     F: 'Infinity',
//     G: undefined,
//     H: undefined,
//     I: undefined,
//     J: undefined,
//     K: undefined,
//     L: undefined
//   },
//   {
//     A: '=SUM(X1:Y10)',
//     B: '=TODAY()',
//     C: '2020/1/1',
//     D: '\'007',
//     E: 'Has Space',
//     F: '日本語',
//     G: 'I say "Yes!"',
//     H: '"Line\r\nBreak"',
//     I: undefined,
//     J: undefined,
//     K: undefined,
//     L: undefined
//   }
// ];
Parameters:
Name Type Attributes Description
csvpath string

The CSV Filepath to read.

options object <optional>

See options and Wsh.Util.conv2DArrayToObj options.

Returns:
  • The array of objects.
Type
Array

(static) readCsvSync(csvpath, optionsopt) → {Array}

Source:

Gets a two dimensions array from the CSV file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

var readArray = fse.readCsvSync('D:\\logs.csv', { encoding: 'sjis' });
// Returns: [
//   ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'],
//   ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
//   ['2020/1/1', '\'007', 'Has Space', '日本語', 'I say "Yes!"', 'Line\r\nBreak', 'Foo,Bar,Baz'],
//   ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
// ]
Parameters:
Name Type Attributes Description
csvpath string

The CSV file path to read.

options object <optional>

See options and Wsh.Util.parseCsvTo2DArray options

Properties
Name Type Attributes Default Description
encoding string <optional>
'utf-8'

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

Returns:
  • The read array.
Type
Array

(static) readdirSyncRecursively(dirPath, optionsopt) → {Array.<string>|Array.<object>}

Source:

Recursively lists all files in a directory and its subdirectories.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

var testDir = 'D:\\testDir';
//  D:\testDir\
//  │  fileRoot1.txt
//  │  fileRoot2-Symlink.log
//  │  fileRoot2.log
//  │
//  ├─DirBar\
//  │  │  fileBar1.txt
//  │  │
//  │  └─DirQuux\
//  │          fileQuux1-Symlink.log
//  │          fileQuux1.txt
//  │
//  ├─DirFoo\
//  └─DirFoo-Symlink\

// Ex.1 No options
fse.readdirSyncRecursively();
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2-Symlink.log', // <SYMLINKD>
//   'fileRoot2.log',
//   'DirBar',
//   'DirFoo',
//   'DirFoo-Symlink', // <SYMLINKD>
//   'DirBar\\fileBar1.txt',
//   'DirBar\\DirQuux',
//   'DirBar\\DirQuux\\fileQuux1-Symlink.log', // <SYMLINKD>
//   'DirBar\\DirQuux\\fileQuux1.txt' ]

// Ex.2 Files only
fse.readdirSyncRecursively(testDir, { isOnlyFile: true });
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2-Symlink.log', // <SYMLINKD>
//   'fileRoot2.log',
//   'DirBar\\fileBar1.txt',
//   'DirBar\\DirQuux\\fileQuux1-Symlink.log', // <SYMLINKD>
//   'DirBar\\DirQuux\\fileQuux1.txt' ]

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

// Ex.4 Excludes Symlink
fse.readdirSyncRecursively(testDir, { excludesSymlink: true });
// Returns: [
//   'fileRoot1.txt',
//   'fileRoot2.log',
//   'DirBar',
//   'DirFoo',
//   'DirBar\\fileBar1.txt',
//   'DirBar\\DirQuux',
//   'DirBar\\DirQuux\\fileQuux1.txt' ]

// Ex.5 Filtering
fse.readdirSyncRecursively(testDir, {
  matchedRegExp: '\\d+\\.txt$',
  ignoredRegExp: 'Root'
});
// Returns:
// ['DirBar\\fileBar1.txt',
//  'DirBar\\DirQuux\\fileQuux1.txt'];

// Ex.6 withFileTypes
fse.readdirSyncRecursively(testDir, { withFileTypes: true });
// Returns: [
// { neme: 'fileRoot1.txt',
//   path: 'D:\\testDir\\fileRoot1.txt,
//   attributes: 32,
//   isDirectory: false,
//   isFile: true,
//   isSymbolicLink: false,
//   ...
// },
// { neme: 'fileRoot2.log',
//   path: 'D:\\testDir\\fileRoot2.log',
//   ...
// },
// ...
// }]
Parameters:
Name Type Attributes Description
dirPath string

The directory path to read.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
isOnlyDir boolean <optional>
false
isOnlyFile boolean <optional>
false
excludesSymlink boolean <optional>
false
matchedRegExp string <optional>

Ex. "\d+\.txt$"

ignoredRegExp string <optional>

Ex. "[_\-.]cache\d+"

withFileTypes boolean <optional>
false

false: ['relative path1', 'relative path1', ...]. true: [{ name: 'full path1', isDirectory: true} ...]

ignoresErr boolean <optional>
false

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

prefixDirName string <optional>
Returns:
  • The array of files info.
Type
Array.<string> | Array.<object>

(static) readdirSyncRecursivelyWithDIR(dirPath, options) → {Array}

Source:

[W.I.P] Recursively lists all files in a directory and its subdirectories with dir command.

Parameters:
Name Type Description
dirPath string

A directory path

options object
Properties
Name Type Attributes Default Description
withFileTypes boolean <optional>
false

false: ['relative path1', 'relative path1', ...]. true: [{ name: 'full path1', isDirectory: true} ...]

matchedRegExp string <optional>

Ex. "\w+\.txt$"

ignoredRegExp string <optional>

Ex. "[_\-.]cache\d+"

Returns:
Type
Array

(static) readJsonSync(fpJson, optionsopt) → {any}

Source:

Gets an Object from the JSON file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

var readObj = fse.readJsonSync('D:\\settings.json');
// Returns: {
//   array: [1, 2, 3],
//   bool: false,
//   float: 3.14,
//   num: 42,
//   obj: { a: 'A' },
//   str: 'Some string',
//   nu: null }
Parameters:
Name Type Attributes Description
fpJson string

The .json file path to read.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
encoding string <optional>
'utf-8'

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:
Type
any

(static) removeSync(fpath) → {void}

Source:

Removes the file or directory. Similar to npm jprichardson/fs-extra

Example
var fse = Wsh.FileSystem; // Shorthand

// File
fse.removeSync('D:\\MyFile.path');

// Directory
fse.removeSync('D:\\MyDir');

// A non-existing directory (Non error)
fse.removeSync('R:\\NonExistingDir');
Parameters:
Name Type Description
fpath string

The file path to remove.

Returns:
Type
void

(static) unzipOfficeOpenXML(srcPath, destDir) → {string}

Source:

Copies files from a directory/zip with Shell.Application. Can also extract a zip file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand

fse.unzipOfficeOpenXML('D:\\MyBook.xlsx', 'R:\\DestDir');
// Result:
//  D:\DestDir\
//  └─ MyBook.xlsx\
//       ├─ ooxml\
//       ├─ docProps\
//       ├─ xl\
//       │  ├─ theme\
//       │  ├─ worksheets\
//       │  └─ _rels\
//       └─ _rels\
Parameters:
Name Type Description
srcPath string

The source OOXML file (e.g. xlsx, docx).

destDir string

The destination directory path.

Returns:
  • The directory path to unzip.
Type
string

(static) writeCsvSync(csvpath, arrays, optionsopt) → {void}

Source:

Writes the two dimensions array to the CSV file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand
var testArray = [
  ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'],
  ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
  ['2020/1/1', '\'007', 'Has Space', '日本語', 'I say "Yes!"', 'Line\nBreak', 'Foo,Bar,Baz'],
  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
];

// Ex.1 The default writing option is utf-8 BOM for Excel.
fse.writeCsvSync('D:\\test.csv', testArray);

// Ex.2 Write as Shift_JIS
fse.writeCsvSync('D:\\test_sjis.csv', testArray, { encoding: 'sjis' });
Parameters:
Name Type Attributes Description
csvpath string

The destination CSV path.

arrays Array

The Array to write.

options object <optional>

See Wsh.Util.stringify2DArrayToCsv options and options.

Properties
Name Type Attributes Default Description
encoding string <optional>
'utf-8'

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

bom boolean <optional>
true

true => enable, others => disable

Returns:
Type
void

(static) writeJsonSync(fpJson, obj, optionsopt) → {void}

Source:

Writes the Object to the JSON file.

Example
var fse = Wsh.FileSystemExtra; // Shorthand
var testObj = {
  array: [1, 2, 3],
  bool: false,
  float: 3.14,
  num: 42,
  obj: { a: 'A' },
  str: 'Some string',
  nu: null
};

fse.writeJsonSync('D:\\test.json', testObj);

fse.writeJsonSync('D:\\test_sjis.json', testObj, {
  indent: '  ',
  lineEnding: '\r\n',
  encoding: 'sjis'
});
Parameters:
Name Type Attributes Description
fpJson string

The destination JSON path.

obj object

The object to write.

options object <optional>

Optional parameters.

Properties
Name Type Attributes Default Description
indent string <optional>
4

"" to 1 liner code(No line break). See space parameter in JSON.stringify options

lineEnding string <optional>
'\n'

The character of line-ending.

encoding string <optional>
'utf-8'

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