end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転しました

指定フォルダ以下を再帰探索して、ファイル一覧を作成するvbscript

http://www.whitire.com/vbs/index.html
↑このurlを参考にさせて頂きながら、書いてみました。

vbscript

Option Explicit

Dim FIND_START_FOLDER
FIND_START_FOLDER = "c:\home\endo\tmp"                  '探索開始folder
Dim FIND_RESULT_FILE_NAME
FIND_RESULT_FILE_NAME = "c:\home\endo\tmp\FIND_RESULT.TXT" '探索結果一覧
Dim FIND_RESULT_FILE_OBJ

Sub Main()

Dim objFSO          ' FileSystemObject

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
'refer to http://msdn.microsoft.com/ja-jp/library/ie/cc428044.aspx
'2=書込用としてopen  , True=file新規作成 , -1=unicodeで書込
Set FIND_RESULT_FILE_OBJ = objFSO.OpenTextFile(FIND_RESULT_FILE_NAME,2,True,-1)


FIND_RESULT_FILE_OBJ.Write("#PATH,SIZE(byte),MODIFY DATE,MODIFY DATE AGE,")
FIND_RESULT_FILE_OBJ.Write("ACCESS DATE,ACCESS DATE AGE")
FIND_RESULT_FILE_OBJ.WriteLine("")

FindFolder objFSO.getFolder(FIND_START_FOLDER)

FIND_RESULT_FILE_OBJ.Close

End Sub


' フォルダ検索関数
Sub FindFolder(ByVal objParentFolder)

Dim objFile
Dim resultLine
For Each objFile In objParentFolder.Files
    FIND_RESULT_FILE_OBJ.Write(objFile.ParentFolder & "\" & objFile.Name & ",")
    FIND_RESULT_FILE_OBJ.Write(objFile.Size & ",") 'byte
    FIND_RESULT_FILE_OBJ.Write(objFile.DateLastModified & ",")
    FIND_RESULT_FILE_OBJ.Write(Fix(Date() - objFile.DateLastModified) & ",")
    FIND_RESULT_FILE_OBJ.Write(objFile.DateLastAccessed & ",")
    FIND_RESULT_FILE_OBJ.Write(Fix(Date() - objFile.DateLastAccessed))
    FIND_RESULT_FILE_OBJ.WriteLine("")

Next

Dim objSubFolder    ' サブフォルダ
For Each objSubFolder In objParentFolder.SubFolders
    FindFolder objSubFolder
Next

End Sub

Main

実行結果

#PATH,SIZE(byte),MODIFY DATE,MODIFY DATE AGE,ACCESS DATE,ACCESS DATE AGE
C:\home\endo\tmp\20120117.txt,949,2012/01/17 17:14:16,0,2012/01/17 17:14:16,0
C:\home\endo\tmp\file_info.vbs,2079,2012/01/17 11:16:04,0,2012/01/18 21:17:37,0
C:\home\endo\tmp\file_list.txt,889,2012/01/18 21:12:55,0,2012/01/18 21:12:55,0
C:\home\endo\tmp\find.vbs,780,2012/01/17 11:14:53,0,2012/01/17 12:47:07,0
C:\home\endo\tmp\FIND_RESULT.TXT,497,2012/01/18 21:25:50,0,2012/01/18 21:25:50,0
C:\home\endo\tmp\foo.css,12,2012/01/18 15:41:49,0,2012/01/18 15:41:49,0
C:\home\endo\tmp\foo.txt,153,2012/01/18 20:06:35,0,2012/01/18 20:06:35,0
C:\home\endo\tmp\link.vbs,125,2012/01/17 11:14:18,0,2012/01/18 20:55:22,0
C:\home\endo\tmp\make_short_cut.vbs,1453,2012/01/18 21:25:41,0,2012/01/18 21:25:41,0
C:\home\endo\tmp\setup_baitai.txt,101,2010/11/16 18:03:23,427,2012/01/18 21:10:46,0
C:\home\endo\tmp\sing_setup.txt,6651,2011/06/16 14:46:57,215,2012/01/18 20:44:11,0
C:\home\endo\tmp\TODO.TXT,524,2012/01/13 16:35:09,4,2012/01/17 12:52:57,0