<! --
referrenced below sites.
https://stackoverflow.com/questions/49456579/running-a-bat-file-using-an-html-button
https://qiita.com/itasnasal/items/c9bd297c6c2050db6d76
https://docs.microsoft.com/ja-jp/previous-versions/windows/scripting/cc364421(v=msdn.10)?redirectedfrom=MSDN
https://maywork.net/computer/powershell-xlsx-search/
-->
<html>
<head>
<title>Run Exe or Batch files with Javascript and HTA</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!--
<HTA:APPLICATION
APPLICATIONNAME="Run Exe or Batch files with Javascript and HTA"
ID="MyHTMLapplication"
VERSION="1.0"/> -->
</head>
<script language="Javascript">
function RunMe(){
// get current path
var path = location.pathname;
fso = new ActiveXObject("Scripting.FileSystemObject");
WshShell = new ActiveXObject("WScript.Shell");
var temp = fso.GetSpecialFolder(2); // 2:%TEMP%
var myself = function(){return fso.OpenTextFile(path).ReadAll();}(path);
// myself = myself.replace(/^[\s\S]*\/\* powershell code from here/m, "" );
// myself = myself.replace(/ powershell code to here \*\/*$/gm, "" );
myself = myself.replace(/^[\s\S]*\/\* powershell code from here/m, "" ).replace(/ powershell code to here.+/, "" );
var tempps1 = fso.BuildPath( temp, "searchFromExcel.ps1" );
var ouf = fso.CreateTextFile( tempps1,true, false );
ouf.Write( myself ); ouf.Close();
var temptxt = fso.BuildPath( temp, "findFromExcel.txt" );
var TargetDir = document.getElementById("TargetDir").value;
var Keyword = document.getElementById("Keyword").value;
if (TargetDir.replace(/^s+|s+$/g,'') == "") { alert("input directory path !"); return }
if (Keyword.replace(/^s+|s+$/g,'') == "") { alert("input Keyword !"); return }
var cmd = "powershell -NoProfile -ExecutionPolicy Unrestricted " + tempps1 + " " + TargetDir + " " + Keyword + " " + temptxt;
WshShell.Run(cmd,1,true);
// fso.DeleteFile(tempps1);
}
</script>
Dir パス : <input name="TargetDir" id="TargetDir"><br><br>
検索文字 : <input name="Keyword" id="Keyword"><br><br><br>
<input type="button" Value="バッチ実行" onClick="RunMe();"
</html>
<!--//////////////////////////////////////////////////////////////////////
/* powershell code from here
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
function Search-Excel
{
param(
[string] $fullPath,
[string] $keyword,
[string] $temptxtName
)
$Excel = New-Object -ComObject Excel.Application
try {
$Excel.Visible = $False
# Get-ChildItem $fullPath -Filter "*.xlsx" -Recurse | % {
$fullPath = $_.FullName
$workbook = $Excel.Workbooks.Open($fullPath)
$workbook.Worksheets | % {
$sheetName = $_.Name
$range = $_.UsedRange
$r = $range.Find($keyword)
if ($r -eq $Null) {
} else {
$firstColumn = $r.Column
$firstRow = $r.Row
do {
$psObj = [PSCustomObject]@{
"FullPath"=$fullPath;
"Sheet" = $sheetName;
"Address" = $r.Address();
"Value" = $r.Value()}
if (Test-Path -Path $temptxtName ) {
Write-Output ($psObj.FullPath + "," + $psObj.Sheet + "," + $psObj.Address + "," + $psObj.Value) | Out-File -Encoding utf8 -Append $temptxtName
} else {
Write-Output ($psObj.FullPath + "," + $psObj.Sheet + "," + $psObj.Address + "," + $psObj.Value) | Out-File -Encoding utf8 $temptxtName
}
$r = $range.FindNext($r)
if ($r -eq $Null)
{
# Get-Content -Path $filepath | Sort-Object | Get-Unique -AsString
break
}
} while ($r.Column -ne $firstColumn -Or $r.Row -ne $firstRow)
}
$workbook.Close() | Out-Null
}
# }
} finally {
$Excel.Quit() | Out-Null
$a = Release-Ref($excel)
}
}
if (-not($MyInvocation.PSCommandPath)) {
$TargetDir = $Args[0]
$Keyword = $Args[1]
$temptxtName = $Args[2]
# Search-Excel $TargetDir $Keyword $temptxtName
Get-ChildItem $TargetDir -Filter "*.xlsx" -Recurse | % { Search-Excel $_.FullName $Keyword $temptxtName}
}
powershell code to here */-->