sh モジュール ...

sh モジュールは、実行環境の調査やファイル・ディレクトリー操作などの便利な関数をそろえています。
sh モジュールを呼び出には、require関数を使います。

例えば
    var sh = require("sh");
    if (sh.exists("d:/work/abc.txt") )
      alert("abc.txt exists");
    else
      alert("abc.txt doesn't exist");

次のようにしても同じ。
    var fileExists = require("sh").exists;
    if (fileExists("d:/work/abc.txt") )
      alert("abc.txt exists");
    else
      alert("abc.txt doesn't exist");

以下の説明で sh が現れたら、

  var sh = require("sh");

で実行された sh オブジェクトであることを留意してください。


sh : プロパティ・関数・・・リスト
attributes関数 ファイル、フォルダーの属性を取得
browse関数 BrowserでURLをopen
compile関数 JavaScriptコードをテストcompileする
copy関数 fileをcopyする
crc32関数 32ビットCRC計算
directories, folders関数 directoryをリストアップ
execute関数 コマンドの実行
exists関数 fileの存在チェック
files関数 fileをリストアップ
getpid関数 現在実行中のプロセスIDを取得
getenv関数 環境変数の値を取得
setenv関数 環境変数の値を設定
getcwd関数 current working dirを取得
setcwd関数 current working dirを設定
getTempFilename関数 一時ファイルのファイル名を作成
getKey関数 registryからデータを取得
getKeyEx関数 (拡張版)registryからデータを取得
getRegName関数 registryのエントリー名を取得
setKey関数 registryへデータを登録
setKeyEx関数 (拡張版)registryへデータを登録
deleteKey関数 registryを削除
mkdir関数 ディレクトリを作成
move関数 fileを移動する
remove関数 ファイル削除
rmdir関数 フォルダー削除
showConsole関数 コンソールWindowの表示/非表示 切り替え
isConsoleVisible関数 コンソールWindowの表示チェック
interactive関数 会話モードでScriptを実行
systemプロパティ shモジュールと同じオブジェクト

attributes 関数 sh...

ファイル、フォルダーの属性を取得

sh.attributes( filename ) : Object

引数
filename : String
属性取得したいファイル、フォルダー名
返値
{ attributes: String, size: Number, date: Date, creation: Date }形式(property名とその型)のObject値を返します。
解説
attributesプロパティは"archive, compressed, directory, hidden, offline, readonly, system, temporary"から構成された文字列となります。

browse 関数 sh...

BrowserでURLをopen

sh.browse( file )

引数
file : String
URL
返値
Browserが起動できたらtrueを返します。起動できなければfalseを返します。
解説
標準登録されているBrowserで、指定したURLをopenします。

compile 関数 sh...

JavaScriptコードをテストcompileする

sh.compile( code )

引数
code : String
JavaScriptコード
返値
compileしてエラーがなければtrueを返します。もしエラーがあれば、エラー部分を出力Windowへ表示してくれます。

copy 関数 sh...

fileをcopyする

sh.copy( source, destination ) : Boolean

引数
source : String
copy元ファイル
destination : String
copy先ファイル
返値
copyできたらtrueを返します。できなけばfalseを返します。
var sh = require("sh");
sh.copy("d:/Adir/source.txt","d:/Bdir/des.txt");

crc32 関数 sh...

32ビットCRC計算

sh.crc32( text [ , seed] ) : Integer

引数
text : String
計算したい文字列
seed : Integer
CRC計算の初期値 省略時-1
返値
32ビットCRC計算の結果を返します。
解説
文字列の32ビットCRCを計算します。引数seed,textの順序を逆にしてもよい。

directories, folders 関数 sh...

directoryをリストアップ

sh.directories( [dirspec] ) : Array
sh.folders( [dirspec] ) : Array

引数
dirspec : String
リストアップしたいdirectoryの仕様。省略時、"*.*"
返値
directoryの名前をリストアップした配列を返します。hiddenや .で始まる directoryは除外されます。
var sh = require("sh");
print( sh.directories("d:/work/*.*") ); // print( sh.folders("d:/work/*.*") );

execute 関数 sh...

コマンドの実行

sh.execute( command[, args[, dir]] ) : Boolean

sh.execute( waittime, command[, args[, dir]] ) : Boolean

引数
command : String
実行したいコマンド名またはプログラム名
args : String
コマンドに渡す引数。複数の引数はスペースで区切って並べる
dir : String
実行current-directoryを指定
waittime : Integer
コマンド実行待ち時間(ms), -1なら終了するまで待つ。
返値
コマンドを起動できたらtrueを返します。できなければfalseを返します。
解説
execute関数はコマンドの呼び出しができたらこの関数を抜けて、次の文を実行します。コマンドの実行とスクリプトの処理は並行実行されます。
var sh = require("sh");
sh.execute("d:/work/OgO", "abc.txt def.txt");

exists 関数 sh...

fileの存在チェック

sh.exists( file ) : Boolean

引数
file : String
file名
返値
fileがあればtrueを返します。なければfalseを返します。
var sh = require("sh");
if (sh.exists("d:/work/abc.txt") )
  alert("abc.txt exists");
else
  alert("abc.txt doesn't exist");

files 関数 sh...

fileをリストアップ

sh.files( [filespec] ) : Array

引数
filespec : String
リストアップしたいfileの仕様。省略時、"*.*"
返値
fileの名前をリストアップした配列を返します。System file や hidden file, .で始まる fileは除外されます。
var sh = require("sh");
print( sh.files("d:/work/*.*");

getpid 関数 sh...

現在実行中のプロセスIDを取得

sh.getpid() : Integer

getenv 関数 sh...

環境変数の値を取得

sh.getenv( name ) : String

引数
name : String
環境変数
返値
環境変数の値
参照
setenv関数

setenv 関数 sh...

環境変数の値を設定

sh.setenv( name_value )

引数
name_value : String
"環境変数=値" の文字列形式で設定します。
参照
getenv関数
解説
この関数を不用意に使用すると実行環境を変えてしまうので、取り扱いに注意してください。

getcwd 関数 sh...

current working dirを取得

sh.getcwd() : String

返値
current working dir名を返します。
参照
setcwd関数

setcwd 関数 sh...

current working dirを設定

sh.setcwd( [directory] ) : String

引数
directory : String
設定したいcurrent working dir名。現在のcurrent working dirが知りたければ引数を省略します。
返値
設定前のcurrent working dir名を返します。
参照
getcwd関数

getTempFilename 関数 sh...

一時ファイルのファイル名を作成

sh.getTempFilename( [pathName, prefix] ) : String

引数
pathName : String
一時ファイルのパス名を指定します。省略した場合、一時ファイル用のディレクトリのパス名を指定したことになります。"."はカレントディレクトリとなります。
prefix : String
ファイル名の接頭辞を指定します。
返値
一時ファイルのファイル名を返します。作成に失敗した場合、空文字列を返します。
解説
この関数で作成された一時ファイルはすべて、JavaScript実行が終了したとき自動的に削除されます。
var sh = require("sh");
var {Stream} = require("jsLab");

var tempFile = sh.getTempFilename();
var stream = new Stream(tempFile);
stream.writeln("1 2 3 4");
stream.close();

getKey 関数 sh...

registryからデータを取得

sh.getKey( section[, name] ) : String

引数
section : String
section名
name : String
key名
返値
registryデータ
 var sh = require("sh");
alert( sh.getKey("HKEY_CLASSES_ROOT/https/shell/open/command") );
参照
setKey関数, deleteKey関数

getKeyEx 関数 sh...

(拡張版)registryからデータを取得

sh.getKeyEx( section[, name] ) : Object

引数
section : String
section名
name : String
key名
返値
type, data, sizeプロパティがsetされたオブジェクトを返します。

typeは REG_NONE (0), REG_SZ (1), REG_EXPAND_SZ (2), REG_BINARY (3), REG_DWORD (4), REG_DWORD_LITTLE_ENDIAN (4), REG_DWORD_BIG_ENDIAN (5), REG_LINK (6), REG_MULTI_SZ (7), REG_RESOURCE_LIST (8), REG_FULL_RESOURCE_DESCRIPTOR (9), REG_RESOURCE_REQUIREMENTS_LIST (10), REG_QWORD (11), REG_QWORD_LITTLE_ENDIAN (11).
これら定数は 例えば var {REG_SZ, REG_DWORD} = require('sh'); で取り出せます。

dataは ArrayBufferオブジェクト.
sizeは dataのバイトサイズ.
参照
setKeyEx関数, deleteKey関数

getRegName 関数 sh...

registryのエントリー名を取得

sh.getRegName( section, index ) : String

引数
section : String
section名
index : Integer
レジストリエントリのインデックス番号を指定
返値
指定したインデックス番号のエントリー名を返します。
なければ、undefined値を返します。
参照
setKeyEx関数, deleteKey関数

setKey 関数 sh...

registryへデータを登録

sh.setKey( section, name, value )

引数
section : String
section名
name : String
key名
value : String
返値
registryへデータが登録されたらtrueを返します。登録できなければfalseを返します。
参照
getKey関数, deleteKey関数

setKeyEx 関数 sh...

(拡張)registryへデータを登録

sh.setKey( section, name, type, data, [size] )

引数
section : String
section名
name : String
key名
type : Integer
データタイプ。
REG_NONE (0), REG_SZ (1), REG_EXPAND_SZ (2), REG_BINARY (3), REG_DWORD (4), REG_DWORD_LITTLE_ENDIAN (4), REG_DWORD_BIG_ENDIAN (5), REG_LINK (6), REG_MULTI_SZ (7), REG_RESOURCE_LIST (8), REG_FULL_RESOURCE_DESCRIPTOR (9), REG_RESOURCE_REQUIREMENTS_LIST (10), REG_QWORD (11), REG_QWORD_LITTLE_ENDIAN (11).
これら定数は 例えば var {REG_SZ, REG_DWORD} = require('sh'); で取り出せます。
data : ArrayBuffer
データ
size : Integer
dataのバイトサイズ。省略した場合 data.byteLength。
返値
registryへデータが登録されたらtrueを返します。登録できなければfalseを返します。
参照
getKeyEx関数, deleteKey関数

deleteKey 関数 sh...

registoryを削除

sh.deleteKey( section [ , name] ) : Boolean

引数
section : String
section名
name : String
key名
返値
registryが削除できたらtrueを返します。削除できなければfalseを返します。
参照
getKey関数, setKey関数, getKeyEx関数, setKeyEx関数

mkdir 関数 sh...

ディレクトリを作成

sh.mkdir( dirname )

引数
dirname : String
新しく作るディレクトリ名。

move 関数 sh...

fileを移動する

sh.move( source, destination, replace ) : Boolean

引数
source : String
copy元ファイル
destination : String
copy先ファイル
replace : Boolean
trueならcopy先ファイルがあっても移動します。falseならcopy先ファイルがあれば移動を中止します。(省略時 : false)
返値
ファイルが移動できたらtrueを返します。できなけばfalseを返します。
var sh = require("sh");
sh.move("a.txt","b.txt");
sh.move("c.txt","b.txt",true); // 強引にファイルを移動

remove 関数 sh...

ファイル削除

sh.remove( filename ) : Boolean

引数
filenam : String
削除したいファイルの名前。
返値
削除できたら、trueを返します。削除できなければ、falseを返します。

rmdir 関数 sh...

フォルダー削除

sh.rmdir( foldername, [allDelete] ) : Boolean

引数
filenam : String
削除したいフォルダーの名前。
allDelete : Boolean
trueならサブフォルダー、ファイルをすべて削除します。falseなら空のフォルダーを削除する。デフォルトはfalse.
返値
削除できたら、trueを返します。削除できなければ、falseを返します。

showConsole 関数 sh...

コンソールWindowの表示/非表示 切り替え

sh.showConsole( [show] )

引数
show : Boolean
showがtrueならコンソールWindowを表示、falseなら非表示にします。引数を省略した場合、show=trueと同じ。
参照
isConsoleVisible関数

isConsoleVisible 関数 sh...

コンソールWindowの表示チェック

sh.isConsoleVisible() : Boolean

返値
コンソールWindowの表示されていればtrueを返します。そうでなければfalseを返します。
参照
showConsole関数

interactive 関数 sh...

会話モードでScriptを実行

sh.interactive( b ) : Boolean

引数
b : Boolean
b がtrueなら、会話モードでScriptを実行します。b がfalseなら、非会話モードでScriptを実行します。
返値
設定前のモードを返します。

system プロパティ sh...

shモジュールと同じオブジェクト

sh.system : Object