VBS : ディレクトリ選択

  フォルダを選択して、フォルダ内のファイル(含フォルダ)を列挙する



一般に良く知られるディレクトリ選択ダイアログですが、直接取得されるオブジェクトは Folder オブジェクトというShell のオブジェクトで、いろいろな使い道があります。普通は、.Self.Path でパスを取得しておしまいの場合が多いのですが、少なくともそのフォルダの中のファイルの情報はすぐ取得できます。

( 他にもオプションを変更して、ファイルを表示したりする事もできますが、選択できないので使い道が無いです )

Selectfolder1

Set Shell = CreateObject( "Shell.Application" )
' ダイアログ表示
Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択", 11, 0 )
' キャンセル
if objFolder is nothing then
	WScript.Quit
end if
if not objFolder.Self.IsFileSystem then
	WScript.Echo "ファイルシステムではありません"
	WScript.Quit
end if

WScript.Echo objFolder.Self.Path

strItems = ""

' フォルダ内のコレクションを取得
Set objFolderItems = objFolder.Items()

' コレクションを列挙
nFiles = objFolderItems.Count
For I = 0 to nFiles - 1
	Set objItem = objFolderItems.Item(I)
	if objItem.isFolder then
		strItems = strItems & objItem.Name & " [Folder]" & vbCrLf
	else
		strItems = strItems & objItem.Name & vbCrLf
	end if
Next

' 全て表示
WScript.Echo strItems


ここで、ファイルシステムでは無いのは「マイ コンピュータ」「マイ ネットワーク」です

Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択", 11, "c:\tmp" ) というようにすると、
ルートが c:\tmp になって他へ移動できません。フルパス以外にも定数があって、&H26 にすると
Program Files ディレクトリです
( http://msdn.microsoft.com/en-us/library/bb774096(VS.85).aspx )

最初の 0 は、親ウインドウハンドルなので通常スクリプトでは使いません。
第3引数は、フラグの組み合わせでカスタマイズされます
VBScript では、フラグの組み合わせは足し算で問題無いです。
( ビット演算する場合は or を使います )
VBScriptで、ビットをスイッチとして使う記述

BrowseForFolder Method
http://msdn.microsoft.com/en-us/library/bb774065(VS.85).aspx
プロパティ
http://msdn.microsoft.com/en-us/library/bb787808(VS.85).aspx

第3引数のフラグ
  
// For finding a folder to start document searching
#define BIF_RETURNONLYFSDIRS   0x0001  
// For starting the Find Computer
#define BIF_DONTGOBELOWDOMAIN  0x0002
// Top of the dialog has 2 lines of text for BROWSEINFO.
//lpszTitle and one line if  
#define BIF_STATUSTEXT         0x0004
// this flag is set.  Passing the message BFFM_
// SETSTATUSTEXTA to the hwnd can set the
// rest of the text.  This is not used with 
// BIF_USENEWUI and BROWSEINFO.lpszTitle gets
// all three lines of text.
#define BIF_RETURNFSANCESTORS  0x0008
// Add an editbox to the dialog
#define BIF_EDITBOX            0x0010   
// insist on valid result (or CANCEL)
#define BIF_VALIDATE           0x0020   
// Use the new dialog layout with the ability to resize
#define BIF_NEWDIALOGSTYLE     0x0040   
// Caller needs to call OleInitialize() before using this API

#define BIF_USENEWUI (BIF_NEWDIALOGSTYLE | BIF_EDITBOX)

// Allow URLs to be displayed or entered.
// (Requires BIF_USENEWUI)
#define BIF_BROWSEINCLUDEURLS  0x0080   
// Add a UA hint to the dialog, in place of the edit box.
// May not be combined with BIF_EDITBOX
#define BIF_UAHINT             0x0100   
// Do not add the "New Folder" button to the dialog. 
// Only applicable with BIF_NEWDIALOGSTYLE.
#define BIF_NONEWFOLDERBUTTON  0x0200   
// don't traverse target as shortcut
#define BIF_NOTRANSLATETARGETS 0x0400   

// Browsing for Computers.
#define BIF_BROWSEFORCOMPUTER  0x1000  
// Browsing for Printers
#define BIF_BROWSEFORPRINTER   0x2000  
// Browsing for Everything
#define BIF_BROWSEINCLUDEFILES 0x4000  
// sharable resources displayed
// (remote shares, requires BIF_USENEWUI)
#define BIF_SHAREABLE          0x8000  
  

ファイルを表示する場合
ファイルを選択する事はできません( Windows2000 ではできたりしたんですが )

  
Set Shell = CreateObject( "Shell.Application" )

on error resume next
Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択(ファイル表示)", 11 + &h4000, 0 )
if Err.Number <> 0 then
	WScript.Echo "ファイルが選択されました"
	WScript.Quit
end if
on error goto 0
if objFolder is nothing then
	WScript.Quit
end if
if not objFolder.Self.IsFileSystem then
	WScript.Echo "ファイルシステムではありません"
	WScript.Quit
end if

WScript.Echo objFolder.Self.Path
  

Selectfolder2






  選択したディレクトリをカレントにコピー



  
Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択", 11, 0 )
if objFolder is nothing then
	WScript.Quit
end if
if not objFolder.Self.IsFileSystem then
	WScript.Echo "ファイルシステムではありません"
	WScript.Quit
end if

Set objThisFolder = Shell.NameSpace( WshShell.CurrentDirectory )

Call objThisFolder.CopyHere( objFolder.Self, 0 )
  



  選択したディレクトリ内のファイルをカレントにコピー

  
Set objFolder = Shell.BrowseForFolder( 0, "フォルダ選択", 11, 0 )
if objFolder is nothing then
	WScript.Quit
end if
if not objFolder.Self.IsFileSystem then
	WScript.Echo "ファイルシステムではありません"
	WScript.Quit
end if

Set objThisFolder = Shell.NameSpace( WshShell.CurrentDirectory )

Call objThisFolder.CopyHere( objFolder.Items(), 0 )
  










  infoboard   管理者用   
このエントリーをはてなブックマークに追加





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ