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 )
  




yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[vbsguide]
Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
24/04/20 02:45:37
InfoBoard Version 1.00 : Language=Perl

1 BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube