2020年02月02日


VBScript(PHP) で氏名をランダムに作成





names.wsf
<JOB>
<COMMENT>
************************************************************
 WEB WSH 実行スケルトン
************************************************************
</COMMENT>

<OBJECT id="WshShell" progid="WScript.Shell" />

<SCRIPT language=VBScript>
' ***********************************************************
' 処理開始
' ***********************************************************

nMax = 500

' ***********************************************************
' Cscript.exe で強制実行
' ***********************************************************
Crun

strName1 = "山川森鈴木高田本多村吉岡松丸杉浦中尾安原野内"
strName2 = "和元雅正由克友浩春冬洋輝"
strName3 = "男也一行樹之"
strName4 = "子代美恵"

str = ""

For i = 1 to nMax
	' 姓1文字目
	nTarget = Random( 1, Len(strName1) )
	strName = Mid( strName1, nTarget, 1 )
	' 1文字目と2文字目が一致したら除外
	nTarget2 = nTarget
	Do while( nTarget = nTarget2 )
		nTarget2 = Random( 1, Len(strName1) )
	Loop
	' 姓2文字目
	strName = strName & Mid( strName1, nTarget2, 1 ) & " "
	' 名1文字目
	nTarget = Random( 1, Len(strName2) )
	strName = strName & Mid( strName2, nTarget, 1 )
	' 性別
	nTarget = Random( 0, 1 )
	' 性別によって名2文字目を決定
	if nTarget = 0 then
		nTarget = Random( 1, Len(strName3) )
		strName = strName & Mid( strName3, nTarget, 1 )
	else
		nTarget = Random( 1, Len(strName4) )
		strName = strName & Mid( strName4, nTarget, 1 )
	end if

	str = str & strName & " / "
	if (i-1) Mod 6 = 5 then
		Wscript.Echo str
		str = ""
	end if

Next

if str <> "" then
	Wscript.Echo str
end if


REM ************************************************
REM 指定範囲の整数の乱数を取得
REM ************************************************
Function Random( nMin, nMax )

	Randomize
	Random = nMin + Int(Rnd * (nMax - nMin + 1))

End function

Function SameRandom( nMin, nMax )

	SameRandom = nMin + Int(Rnd * (nMax - nMin + 1))

End function

REM **********************************************************
REM Wscript で実行された場合は Cscript で実行しなおす
REM **********************************************************
Function Crun( )

	Dim str

	str = WScript.FullName
	str = Right( str, 11 )
	str = Ucase( str )
	if str <> "CSCRIPT.EXE" then
		str = WScript.ScriptFullName
		strParam = " "
		For I = 0 to Wscript.Arguments.Count - 1
			if instr(Wscript.Arguments(I), " ") < 1 then
				strParam = strParam & Wscript.Arguments(I) & " "
			else
				strParam = strParam & Dd(Wscript.Arguments(I)) & " "
			end if
		Next
		Call WshShell.Run( "cmd.exe /c cscript.exe " & Dd(str) & strParam & " & pause", 3 )
		WScript.Quit
	end if

End Function

REM **********************************************************
REM ダブルクォートで囲む
REM **********************************************************
Function Dd( strValue )

	Dd = """" & strValue & """"

End function

</SCRIPT>
</JOB>


names.php
<?php
// ソースコードは UTF8N
// PHP.INI の default_charset = "UTF-8"

$NAME_1 = 0;
$NAME_2 = 1;
$NAME_3 = 2;
$NAME_4 = 3;

# シード
$name_seed = array();
$name_seed[] = "山川森鈴木高田本多村吉岡松丸杉浦中尾安原野内";
$name_seed[] = "和元雅正由克友浩春冬洋輝";
$name_seed[] = "男也一行樹之";
$name_seed[] = "子代美恵";
$names = array();

# 人数
$num = 100;

for ( $i = 0; $i < $num; $i++ ) {

	// 姓1文字目
	$n1 = mb_substr( $name_seed[$NAME_1], rand( 0, mb_strlen($name_seed[$NAME_1])-1 ), 1 );

	// 1文字目と2文字目が一致したら除外
	$n2 = $n1;
	while( $n2 == $n1 ) {
		$n2 = mb_substr( $name_seed[$NAME_1], rand( 0, mb_strlen($name_seed[$NAME_1])-1 ), 1 ) ;
	} 

	
	$name_result = $n1 . $n2 . " ";

	// 名1文字目
	$n3 = mb_substr( $name_seed[$NAME_2], rand( 0, mb_strlen($name_seed[$NAME_2])-1 ), 1 );
	$name_result .= $n3;

	$sex = rand( 0,1 );

	// 性別によって名2文字目を決定
	if ( $sex == 0 ) {
		$name_result .= mb_substr( $name_seed[$NAME_3], rand( 0, mb_strlen($name_seed[$NAME_3])-1 ), 1 );
	}
	else {
		$name_result .= mb_substr( $name_seed[$NAME_4], rand( 0, mb_strlen($name_seed[$NAME_4])-1 ), 1 );
	}

	// php PHP 7.3.13 で化けません
	if ( array_search( $name_result, $names ) === FALSE ) {
		print $name_result;
	}
	else {
		// 同姓同名
		print "*" . $name_result;
	}

	print " / ";

	if ( $i % 6 == 5 ) {
		print "\n";
	}

	$names[] = $name_result;

}

file_put_contents("names.log", print_r($names, true) );

print "\n";
print file_get_contents("names.log");

?>


▼ names.bat
php.exe "%~p0names.php"
pause






posted by at 2020-02-02 15:22 | サンプル | このブログの読者になる | 更新情報をチェックする

2019年03月01日


VBScript : 30秒後のイベント処理

30と言う秒数は、サンプルです。最もテストしやすい間隔なので使用しています。

' このセクションは、cscript.exe で処理を強制させるものです
str = WScript.FullName
str = Right( str, 11 )
str = Ucase( str )
if str <> "CSCRIPT.EXE" then
	str = WScript.ScriptFullName
	Set WshShell = CreateObject( "WScript.Shell" )
	Call WshShell.Run( "cmd.exe /c cscript.exe """ & str & """ & pause", 3 )
	WScript.Quit
end if

' 現在の日付・時刻を取得
dtBase = Now()
dtTarget = dtBase
' 30秒後の日付・時刻を取得
dtTarget = DateAdd( "s", 30, dtTarget )

' Win32_LocalTime に対して比較する為の値を作成
TargetH = Hour( dtTarget )
TargetM = Minute( dtTarget )
TargetS = Second( dtTarget )

' WMI の処理の為のオブジェクトを取得
Set obj = GetObject("winmgmts:\\.\root\cimv2")
' 該当する時間になればイベント発生
Set objMonitor = obj.ExecNotificationQuery( _
	"select * from __InstanceModificationEvent " & _
	"where TargetInstance isa 'Win32_LocalTime' " & _
	"and TargetInstance.Hour = " & TargetH & " " & _
	"and TargetInstance.Minute = " & TargetM & " " & _
	"and TargetInstance.Second = " & TargetS & " " _
)

Wscript.Echo "イベントを待機しています..."
' 以下の行で処理が停止します
Set objLatestEvent = objMonitor.NextEvent 

' イベントが発生したら、ここ以下が実行される
Wscript.Echo "イベントが発生しました"
Wscript.Echo dtTarget
Wscript.Echo Now()

以下では、イベントに関する解説と、ログオフさせる方法について書かれています

スクリプトセンターのドキュメント

あるプロセスを開始し、そのプロセスが終了した時点でユーザーをログオフさせる



posted by at 2019-03-01 13:39 | WMI | このブログの読者になる | 更新情報をチェックする

2017年04月30日


VBScript : Excel の新しいBookを作成する


' ****************************************
' 新しい Excel の Book を作成する
' ****************************************
Function NewBook( BookPath )

	Dim ExcelApp	' アプリケーション
	Dim ExcelBook	' ブック

	Set ExcelApp = CreateObject("Excel.Application")

	' 警告を出さないようにする
	ExcelApp.DisplayAlerts = False

	' ブック追加
	ExcelApp.Workbooks.Add

	' 追加したブックを取得
	Set ExcelBook = ExcelApp.Workbooks( ExcelApp.Workbooks.Count )

	on error resume next
	' 保存
	' 56 は、Excel 2007 で拡張子 .xls で保存する場合に必要
	if CLng(Left(ExcelApp.Version & "",2 )) > 11 then
		Call ExcelBook.SaveAs( BookPath, 56 )
	else
		Call ExcelBook.SaveAs( BookPath )
	end if
	if Err.Number <> 0 then
		MsgBox( "ERROR:" & Err.Description )
	end if
	on error goto 0

	' Excel をアプリケーションとして終了
	ExcelApp.Quit
	' Excel を VBScript から開放
	Set ExcelApp = Nothing
	' オブジェクト変数を通常変数として初期化
	' ( ExcelApp がローカル変数なので実際は必要ありません )
	ExcelApp = Empty

End Function

関連する Microsoft のドキュメント

XlFileFormat Enumeration [Excel 2007 Developer Reference]

▼ オンラインライブラリでのテストコード
<JOB>
<SCRIPT language="VBScript" src="http://lightbox.on.coocan.jp/webwsh/GetScriptDir.txt"></SCRIPT>
<SCRIPT language="VBScript" src="http://lightbox.on.coocan.jp/webwsh/NewBook.txt"></SCRIPT>

<SCRIPT language=VBScript>
' ************************************
' 処理開始
' ************************************

	NewBook( GetScriptDir() & "\新しいBOOK.xls" )

</SCRIPT>
</JOB>

http://lightbox.on.coocan.jp/webwsh/GetScriptDir.txt
http://lightbox.on.coocan.jp/webwsh/NewBook.txt

関連する記事

VBScript : Excel のバージョン表示



posted by at 2017-04-30 21:29 | ツール関数 | このブログの読者になる | 更新情報をチェックする