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年4月 | トップページ | 2010年6月 »

2010年5月30日 (日)

PowerShellで文字コードと改行文字を変更する。

例えば、Shift_JIS/CR+LFからEUC/LFに変換するには、

powershell "[text.encoding]::getencoding('euc-jp').getbytes((get-content sjis.txt -encoding string) -join \"`n\")|set-content -encoding byte euc.txt"

あるいは、

powershell "$r=New-Object IO.StreamReader('sjis.txt',[Text.Encoding]::GetEncoding('Shift-JIS'));$w=New-Object IO.StreamWriter('euc.txt',$false,[Text.Encoding]::GetEncoding('EUC-JP'));$w.NewLine=\"`n\";while(!$r.EndOfStream){$w.WriteLine($r.ReadLine());}$r.Close();$w.Close();"

2010年5月29日 (土)

実ファイル名、実パス名は何?

desktop.iniで表示名が変えられていると、エクスプローラで見えるのは、表示名で、実名が分かりません。:-(

「パスとしてコピー」して、どこかに貼り付けて見る?

実パス名は、プロパティのセキュリティタブのオブジェクト名で分かります。

実ファイル名だけなら、詳細表示のファイル名カラムで分かります。

2010年5月24日 (月)

powershellからファイルへのリダイレクションの文字コード

powershell "echo 日本語">a.txt
→ shift_jis

powershell "echo 日本語 >a.txt"
→ unicode

前者は、[console]::OutputEncodingに依存し、デフォルトはshift_jisです。
後者は、unicode固定です。

前者は変更できる文字コードに制限あり。また、元に戻さないと変更が永続するので注意が必要です。

powershell "$encoding=[console]::OutputEncoding;[console]::OutputEncoding=[text.encoding]::getencoding('utf-8');echo 日本語;[console]::OutputEncoding=$encoding;">a.txt

後者は、
> a.txt

| out-file -encoding default a.txt
| set-content -encoding string a.txt
などで代替します。

powershell "echo 日本語 | out-file -encoding default a.txt"
powershell "echo 日本語 | set-content -encoding string a.txt"

2010年5月23日 (日)

powershellから外部アプリへのパイプの文字コード

powershell "echo 日本語" | more.com
→ 日本語

powershell "echo 日本語 | more.com"
→ ???
文字化けします。

前者は、[console]::OutputEncodingに依存し、デフォルトはshift_jis
後者は、$OutputEncodingに依存し、デフォルトはus-ascii

なので、
powershell "$outputencoding=[text.encoding]::getencoding('shift_jis');echo 日本語 | more.com"
→ 日本語

2010年5月21日 (金)

フォルダの詳細表示を印刷する。保存する。(その2)

KB321379 Windows XP のフォルダにディレクトリの印刷機能を追加する方法

これは、バッチファイルなので、コンソールが開き、さらに、一時ファイルも作ります。:-(

もっとスマートに、バッチファイルを作らず、コンソールを開かず、一時ファイルも使わないでできないものでしょうか?

directoryやdirectory\backgroundに関連付けるには、

mshta.exe vbscript:space(createobject("wscript.shell").run("cmd /c dir ""%1""|clip",0,True))+"<textarea style='width:100%;height:100%;'>"+vbcrlf+clipboarddata.getdata("text")+"</textarea>"

※directory\backgroundのときは%V

ショートカットのリンク先には、

cmd.exe /c for /l %n in (1,1,2) do if %n==2 (dir %x%|start /b mshta.exe vbscript:"<textarea style='width:100%;height:100%;'>"+vbcrlf+createobject("scripting.filesystemobject"^).getstandardstream(0^).readall+"</textarea>") else set x=

ショートカットの実行時の大きさは最小化にします。

一時ファイルの代わりに、前者はクリップボード、後者はパイプを利用します。

印刷は、(^P)または右クリック「印刷」。
保存は、右クリック「ソースの表示」から「名前を付けて保存」。拡張子は.htmに変える。

2010年5月20日 (木)

フォルダの詳細表示を印刷する。保存する。

Folder.hta [フォルダパス]

<html>
<head><hta:application id="hta"></hta:application>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<title>Folder.hta</title>
<script language=vbscript><!--
Option Explicit
Dim arg
Dim fQuoting
Dim k
fQuoting=False
arg=hta.commandline
For k=1 To Len(arg)
  Select Case Mid(arg,k,1)
  Case Chr(34) fQuoting=Not fQuoting
  Case Chr(32) If Not fQuoting Then Exit For
  End Select
Next
arg=Mid(arg,k+1)
arg=Trim(Replace(arg,Chr(34),Empty))

Dim Shell
Set Shell=CreateObject("Shell.Application")

Sub window_onload()
Dim Folder
If arg="" Then
  Set Folder=Shell.BrowseForFolder(0,"フォルダ選択",0)
  If Not Folder Is Nothing Then Call FileList(Folder.Self.Path,Folder)
Else
  Call FileList(arg,Shell.NameSpace(arg))
End If
End Sub

Sub FileList(arg,Folder)
Const M=350
Dim COLS
Dim n
Dim k
Dim FolderItem
Dim script
Dim HTML
Dim charset

COLS=Array("名前","サイズ","更新日時","種類")

For n=0 To UBound(COLS)
  For k=0 To M
    If Folder.GetDetailsOf(,k)=COLS(n) Then
      COLS(n)=k
      Exit For
    End If
  Next
Next

document.open
document.writeln "<html>"
document.writeln "<head>"
document.writeln "<meta http-equiv=""Content-Type"" content=""text/html; charset=Shift_JIS"">"
document.writeln "<meta name=""GENERATOR"" content=""FileList.hta"">"
document.writeln "<title>" & arg & "</title>"
document.writeln "</head>"
document.writeln "<body>"
document.writeln "<table BORDER>"
document.writeln "<caption>" & arg & "</caption>"

document.write "<tr>"
For Each k In COLS
  document.write "<td>"
  document.write Folder.GetDetailsOf(,k)
  document.write "</td>"
Next
document.writeln "</tr>"

For Each FolderItem In Folder.Items
  document.write "<tr>"
  For Each k In COLS
    document.write "<td>"
    document.write Replace(Replace(Folder.GetDetailsOf(FolderItem,k),ChrW(8206),""),ChrW(8207),"")
    document.write "</td>"
  Next
  document.writeln "</tr>"
Next
document.writeln "</table>"
document.writeln "</body>"
document.writeln "</html>"
document.close
document.charset="shift_jis"
End Sub
--></script>
</head>
<body>
</body>
</html>

印刷は、(^P)または右クリック「印刷」。
保存は、右クリック「ソースの表示」から「名前を付けて保存」。拡張子は.htmに変える。
フォルダ選択は、(F5)または右クリック「最新の情報に更新」。

起動は、ダブルクリックまたはフォルダをドロップ。
あるいは、sendtoフォルダに入れてフォルダを「送る」。
directoryに関連付けて、フォルダの右クリックで選択。
mshta.exe "パス\Folder.hta" "%1"

2010年5月16日 (日)

IEのキャッシュフォルダから指定サイズ以上の大きいファイルを探す。

1MB以上のファイルを探すなら、

cache_size.cmd 1048576

@echo off
if  not "%~1"=="" (
for %%J in (
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5"
"%USERPROFILE%\AppData\Local\Temp\Temporary Internet Files\Content.IE5"
) do for /f "delims=" %%K in ('dir /a /b /s %%J') do for %%I in ("%%K") do if %%~zI geq %1 echo %%~ftzaI
)

サイズをバイト単位で指定します。

2010年5月12日 (水)

IEのキャッシュフォルダからSWFファイルなどを探す。取り出す。

ファイル名(ワイルドカード使用可)を指定して、ディレクトリ情報を表示します。

cache_dir.cmd *.swf

@echo off
if  not "%~1"=="" (
for %%J in (
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5"
"%USERPROFILE%\AppData\Local\Temp\Temporary Internet Files\Content.IE5"
) do for /f "delims=" %%K in ('dir /a /b /s "%%~J\%~1"') do for %%I in ("%%K") do echo %%~ftzaI
)

カレントディレクトリに取り出します。ファイル名はフルパスから\を_に変えて生成します。

cache_copy.cmd *.swf

@echo off
if  not "%~1"=="" (
for %%J in (
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5"
"%USERPROFILE%\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5"
"%USERPROFILE%\AppData\Local\Temp\Temporary Internet Files\Content.IE5"
) do for /f "delims=" %%K in ('dir /a /b /s "%%~J\%~1"') do (
set x=%%K
call set x=%%x::=%%
call set x=%%x:\=_%%
call copy "%%K" "%%x%%"
)
)

2010年5月10日 (月)

ひとつのスクリプトから同時に複数のMsgBoxを出す。

普通はひとつのMsgBoxしか出せませんが、

msgbox3.vbs

set d=createobject("htmlfile")
d.parentwindow.settimeout getref("proc1"),400,"vbscript"
d.parentwindow.settimeout getref("proc2"),200,"vbscript"
msgbox 3

sub proc1
msgbox 1
end sub

sub proc2
msgbox 2
end sub

MsgBoxはModal(?)なようです。操作できるウィンドウは最前面のひとつです。使えない。。。

一方、WScript.ShellのPopupはModeless(?)なようです。

popup3.vbs

set d=createobject("htmlfile")
d.parentwindow.settimeout getref("proc1"),400,"vbscript"
d.parentwindow.settimeout getref("proc2"),200,"vbscript"
createobject("wscript.shell").popup  3

sub proc1
createobject("wscript.shell").popup  1
end sub

sub proc2
createobject("wscript.shell").popup  2
end sub

Popupのウィンドウは独立していて、タスクバーにも別々に現れます。

2010年5月 9日 (日)

コマンドプロンプトでCALLと^を使って遅延展開する。

バッチファイルでは、CALLと%%を使って遅延展開ができました。

for %%i in (aaa bbb ccc) do (
set xxx=%%i
call echo %%xxx%% "%%xxx%%"
)

しかし、この手はコマンドプロンプトで使えませんでした。

しかし、^を使えばできます。例えば、%^xxx%、%xxx^%、...など。

for %i in (aaa bbb ccc) do (
set xxx=%i
call echo %xxx^% ^"%xxx^%^"
)

しかし、本当は、^を使わなくても、CALLだけでもできます。

set xxx=
for %i in (aaa bbb ccc) do (
set xxx=%i
call echo %xxx% "%xxx%"
)

要は1回目の解析パスで環境変数の名前が一致しなければよいのです。
%環境変数%の置換処理は^のエスケープ処理より先なので、環境変数の名前を擬装するのに^が使えます。
擬装した名前があれば駄目ですが。。。
また、"~"で囲まれても駄目です。
set xxx^^=aaa
set xxx=bbb
echo %xxx^% → aaa
echo "%xxx^%" → "aaa"
---
set xxx^^=
set xxx=bbb
echo %xxx^% → %xxx%
echo "%xxx^%" → "%xxx^%"
call echo %xxx^% → bbb
call echo "%xxx^%" → "%xxx^^%"
call echo ^"%xxx^%^" → "bbb"

2010年5月 8日 (土)

HTAやHTML、Excel VBAなどでWScript.ShellのPopup(時間指定)を使う。

マウスがPopupウィンドウの上にあれば効きます。
マウスがHTAウィンドウの上にあれば効きません。
経過時間タイマにすべきところを、待ち時間タイマにしているため、他のウィンドウにマウスがあると、CPUが動いて、待ち時間タイマがリセットされる?

この仮説を確認する.vbs

set d=createobject("htmlfile")
d.parentwindow.setinterval getref("proc"),1000
createobject("wscript.shell").popup "5",5
sub proc
end sub

結果、待ちが解けません。

そこで、
CreateObject("WScript.Shell").popup("5秒後に閉じたい",5)
の代わりに、
CreateObject("WScript.Shell").Run "mshta.exe vbscript:execute(""close(CreateObject(""""WScript.Shell"""").popup(""""5秒後に閉じます"""",5))"")"

2010年5月 7日 (金)

MHTMLファイルを分解する。

MHTMLファイル「ほげ.mht」を分解して、フォルダ「ほげ」の下に展開します。
ファイル名は、第一階層番号[.第二階層番号[...]].txt です。
ファイル名とURLの対応関係を index.htm に書き出します。

ExpandMHTML.vbs MHTMLファイル...

Option Explicit
Dim fso
Dim Path
Dim FolderName
Set fso=CreateObject("Scripting.FileSystemObject")
For Each Path In WScript.Arguments
  FolderName=fso.BuildPath(fso.GetParentFolderName(Path),fso.GetBaseName(Path))
  If fso.FolderExists(FolderName) Then
    For k=2 To 9
      If Not fso.FolderExists(FolderName & " " & k) Then Exit For
    Next
    FolderName=FolderName & " " & k
  End If
  fso.CreateFolder FolderName
  Call Expand(Path,FolderName)
Next

Sub Expand(Path,Folder)
Dim Stm
Dim Msg
Dim File
Set Stm=CreateObject("ADODB.Stream")
Stm.Open
Stm.LoadFromFile Path
set Msg=createobject("CDO.Message")
Msg.DataSource.OpenObject Stm, "_Stream"
Set File=fso.CreateTextFile(Folder & "\index.html")
File.WriteLine "<table border><thead><td>File</td><td>Content-Type</td><td>Content-Location</td></thead><tbody>"
Call SaveToFile(Msg.BodyPart,"1",Folder,File)
File.WriteLine "</tbody></table>"
File.Close
End Sub

Sub SaveToFile(BodyPart,BaseName,Folder,File)
Dim k
If BodyPart.BodyParts.Count Then
  For k=1 To BodyPart.BodyParts.Count
    Call SaveToFile(BodyPart.BodyParts.Item(k),BaseName & "." & k,Folder,File)
  Next
Else
  File.WriteLine "<tr><td>" & BaseName & "</td><td>" & BodyPart.Fields.Item("urn:schemas:mailheader:content-type") & "</td><td>" &  fso.GetFileName(BodyPart.Fields.Item("urn:schemas:mailheader:content-location")) & "</td><td>" & BodyPart.Fields.Item("urn:schemas:mailheader:content-location") & "</td></tr>"
  BodyPart.SaveToFile Folder & "\" & BaseName & ".txt"
End If
End Sub

2010年5月 1日 (土)

制限付きサイトの汎用Mark Of The Web(MOTW)は?

以前は、
<!-- saved from url=(0007)http:// -->
で制限付きサイトになりましたが、今はインターネットになるようです。

今は、
<!-- saved from url=(0005)http: -->
で制限付きサイトになります。

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