2022年5月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
無料ブログはココログ

« 2010年6月 | トップページ | 2010年8月 »

2010年7月27日 (火)

バッチファイルの中でコードページをUTF-8に変える。

コードページ:65001 (UTF-8)の間は、バッチファイルが読めないので、変えて戻すまでを1行(または複文)に書きます。
また、コードページを変えると画面がクリアされるので、画面を確認するためにchcpの前にpauseを入れます。

test1.cmd

echo 前前前
pause & chcp 65001 & cmd /c echo 中中中 & pause & chcp 932
echo 後後後

これでは、効果がよく見えないので、まず、UTF-8のファイルを用意します。

chcp 65001 & cmd /c echo 中中中>utf8.txt & chcp 932

これをコードページ:932(シフトJIS)で表示すると、文字化けして見えます。

type utf8.txt

それをコードページ:65001 (UTF-8)で表示するバッチファイルです。

test2.cmd

echo 前前前
(
pause
chcp 65001
cmd /c type utf8.txt
pause
chcp 932
)
echo 後後後

2010年7月21日 (水)

コンソールアプリか、Windowsアプリか、調べる。

GetBinaryType.vbs ファイル

Set fso=CreateObject("Scripting.FileSystemObject")
Buf=fso.OpenTextFile(WScript.Arguments.Item(0),1,False,True).Read(256)
Position=AscB(MidB(Buf,1+&H3c,1))+AscB(MidB(Buf,2+&H3c,1))*256+AscB(MidB(Buf,3+&H3c,1))*256*256+AscB(MidB(Buf,4+&H3c,1))*256*256*256+(&H4+&H14+&H44)
Subsystem=AscB(MidB(Buf,1+Position,1))
Select Case Subsystem
Case 2 WScript.Echo "GUI"
Case 3 WScript.Echo "CUI"
Case Else WScript.Echo Subsystem
End Select
WScript.Quit Subsystem

2010年7月20日 (火)

PowerShellからテキストをIEウィンドウに表示する。

HTMLでなく、テキストをIEウィンドウに表示します。

out-ie-text.ps1

$ie=new-object -com InternetExplorer.Application
$ie.toolbar=$false
$ie.visible=$true
$ie.navigate("about:blank")
while($ie.busy -or ($ie.readystate -ne 4)){sleep 1}
$html=$input|out-string
$ie.document.open("text/plain")|out-null
try{
  $ie.document.write($html)
}catch{
  $ie.document.IHTMLDocument2_write($html)
}
$ie.document.close()
try{
  $ie.document.parentwindow.focus()
}catch{
  $sc=new-object -com scriptcontrol
  $sc.language="vbscript"
  $sc.addobject("ie",$ie)
  $sc.executestatement("ie.document.parentwindow.focus()")
}

2010年7月19日 (月)

out-ie.ps1 PowerShellからHTMLをIEに流し込む。(その2)

IEウィンドウがタスクバーで点滅するので、前面に持っていきます。

out-ie.ps1

$ie=new-object -com InternetExplorer.Application
$ie.toolbar=$false
$ie.visible=$true
$ie.navigate("about:blank")
while($ie.busy -or ($ie.readystate -ne 4)){sleep 1}
$html=$input|out-string
try{
  $ie.document.write($html)
}catch{
  $ie.document.IHTMLDocument2_write($html)
}
try{
  $ie.document.parentwindow.focus()
}catch{
  $sc=new-object -com scriptcontrol
  $sc.language="vbscript"
  $sc.addobject("ie",$ie)
  $sc.executestatement("ie.document.parentwindow.focus()")
}

2010年7月18日 (日)

PowerShellでは、MTAで、$ie.document.parentwindowが見えない。

STAでは見えます。なので、PowerShell_ISEで見えます。

ps1ファイルでは、MTAならSTAで起動し直します。

if([Threading.Thread]::CurrentThread.GetApartmentState() -eq "MTA"){
  PowerShell -Sta -File $MyInvocation.MyCommand.Path
  exit
}
$ie=new-object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("about:blank")
while($ie.busy -or ($ie.readystate -ne 4)){sleep 1}
$ie.document.parentwindow.focus()

あるいは、ScriptControlで逃げます。

$ie=new-object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("about:blank")
while($ie.busy -or ($ie.readystate -ne 4)){sleep 1}
$sc=new-object -com scriptcontrol
$sc.language="vbscript"
$sc.addobject("ie",$ie)
$sc.executestatement("ie.document.parentwindow.focus()")

2010年7月17日 (土)

PowerShellのConvertTo-HTML出力をHTAウィンドウに表示、印刷、名前を付けて保存する。

例えば、PowerShellのdirコマンドの出力をConvertTo-HTMLで加工してHTAウィンドウに表示します。

htaファイルでは、

<script defer>
document.open();
document.write(new ActiveXObject('wscript.shell').exec('cmd /c <nul powershell "dir|convertto-html -property Mode,Name,Length,LastWriteTime,Attributes|write-host"').stdout.readall());
document.close();
</script>

または、

<script defer>
new ActiveXObject('wscript.shell').run('powershell -Sta "Add-Type -AssemblyName System.Windows.Forms;[Windows.Forms.Clipboard]::SetText((dir|convertto-html -property Mode,Name,Length,LastWriteTime,Attributes|out-string))"',0,true);
document.open();
document.write(clipboardData.getData("text"));
document.close();
</script>

前者はコンソールウィンドウが一時的に開きます。後者はクリップボードを利用します。

コマンドプロンプトやバッチファイルでは、

powershell "dir|convertto-html -property Mode,Name,Length,LastWriteTime,Attributes|write-host"|mshta.exe "about:<script defer>document.write(new ActiveXObject('scripting.filesystemobject').getstandardstream(0).readall());</script>"

パイプが同期で待ち合わせするので、待ち合わせないで非同期にするには、

powershell ~~~|start /b mshta.exe ~~~

印刷は、右クリック、印刷。

保存は、右クリック、ソースの表示、名前を付けて保存。

2010年7月16日 (金)

PowerShellの外部への出力が79桁で折り返される。

powershell "echo '000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888'"

ここで、折り返されないようにするには、write-hostを使います。

powershell "echo '000000000011111111112222222222333333333344444444445555555555666666666677777777778888888888'|write-host"

2010年7月15日 (木)

PowerShellのワンライナをWScript.ShellのExec()すると、終了待ちが解けない。

例えば、

MsgBox CreateObject("WScript.Shell").Exec("PowerShell ""dir""").StdOut.ReadAll

は、コンソールが開いたまま動きません。コンソールを閉じると動きます。

PowerShellが標準入力を待つからのようです。

この待ちを解くには、スクリプトから、StdIn.Closeするか、あるいは、コマンドラインの先頭に、"cmd /c <nul "を付けます。

Set oExec=CreateObject("WScript.Shell").Exec("PowerShell ""dir""")
oExec.StdIn.Close
MsgBox oExec.StdOut.ReadAll

または、

MsgBox CreateObject("WScript.Shell").Exec("cmd /c <nul PowerShell ""dir""").StdOut.ReadAll

2010年7月14日 (水)

コンソールコマンド出力をHTAウィンドウにテキスト形式で表示、印刷、名前を付けて保存する。

例えば、dirコマンドの出力をHTAウィンドウに表示します。

htaファイルでは、

<script defer>
document.open('text/plain');
document.write(new ActiveXObject('wscript.shell').exec('cmd /c dir').stdout.readall());
document.close();
</script>

または、

<script defer>
new ActiveXObject('wscript.shell').run('cmd /c dir|clip',0,true);
document.open('text/plain');
document.write(clipboardData.getData('text'));
document.close();
</script>

前者はコンソールウィンドウが一時的に開きます。後者はクリップボードを利用します。

コマンドプロンプトやバッチファイルでは、

dir|mshta.exe "about:<script defer>document.open('text/plain');document.write(new ActiveXObject('scripting.filesystemobject').getstandardstream(0).readall());document.close();</script>"

パイプが同期で待ち合わせするので、待ち合わせないで非同期にするには、

dir|start /b mshta.exe ~~~

印刷は、右クリック、印刷。

保存は、右クリック、ソースの表示、名前を付けて保存。

2010年7月13日 (火)

HTAウィンドウにテキストファイルを表示する。

IEでは、テキストファイル(.txt)を開くと、テキストのまま表示されます。
しかし、HTAでは、テキストファイル(.txt)を開くと、HTMLとして表示されます。
HTAでは、テキストのまま表示できないのでしょうか?
いいえ、deferやonloadなどで、document.open("text/plain")してwrite,closeすれば、できます。

2010年7月12日 (月)

コンソールコマンド出力をHTAウィンドウの<textarea>内に表示、印刷、名前を付けて保存する。

例えば、dirコマンドの出力をHTAウィンドウの<textarea>内に表示します。

htaファイルでは、

<script defer>
t1.value=new ActiveXObject('wscript.shell').exec('cmd /c dir').stdout.readall()
</script>
<textarea id=t1 style='width:100%;height:100%;'></textarea>

または、

<script defer>
new ActiveXObject('wscript.shell').run('cmd /c dir|clip',0,true);
t1.value=clipboardData.getData('text');
</script>
<textarea id=t1 style='width:100%;height:100%;'></textarea>

前者はコンソールウィンドウが一時的に開きます。後者はクリップボードを利用します。

コマンドプロンプトやバッチファイルでは、

dir|mshta.exe vbscript:"<textarea style='width:100%%;height:100%%;'>"+vbcrlf+createobject("scripting.filesystemobject").getstandardstream(0).readall+"</textarea>"

パイプが同期で待ち合わせするので、待ち合わせないで非同期にするには、

dir|start /b mshta.exe ~~~

印刷は、右クリック、印刷。

保存は、htaファイルでは不可。バッチの場合、右クリック、ソースの表示、名前を付けて保存。※<textarea>タグが付きます。

« 2010年6月 | トップページ | 2010年8月 »