WSHでファイルを操作する方法。
特定のディレクトリ以下のサブディレクトリを取得する方法
Option Explicit
Dim oFSO
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")
Dim oDir
Set oDir = oFSO.GetFolder("C:\Local_Files\Backups")
Dim vLog
vLog = ""
Dim oFile
For Each oFile In oDir.Files
vLog = vLog & oFile.Name & vbCrLf _
& " サイズ : " & CStr(oFile.Size) & "[Byte]" & vbCrLf _
& " 作成時刻 : " & oFile.DateCreated & vbCrLf _
& " 更新時刻 : " & oFile.DateLastModified & vbCrLf _
& vbCrLf
Next
Call MsgBox(vLog)
'片付け
Set oFile = Nothing
Set oDir = Nothing
Set oFSO = Nothing
バッチ処理の結果に、ファイルの簡単なプロパティなんかを
メールを送信する際に便利。
ショートカットを検索して、正しくアクセスできるかどうか検査するスクリプト
Option Explicit
Const EXIT_WITH_OK = 0
Const EXIT_WITH_APPLICATION_ERROR = 1
Const EXIT_WITH_USING_ERROR = 2
Sub ShowUsage()
Dim strUsage
strUsage = _
"[使い方]" & vbCrLf & _
" CheckShortcuts.vbs [<パス>]" & vbCrLf & _
" <パス> 検査対象のパスを指定します。" & vbCrLf & _
" 省略した場合は現在のパスを指定します。"
Call WScript.Echo(strUsage)
End Sub
Function GetExtension(ByVal pPath)
Dim vPos, vLength, vExt
vPos = InstrRev(pPath, ".")
If (vPos <> 0) Then
vLength = Len(pPath) - vPos
vExt = Right(pPath, vLength)
Else
vExt = ""
End If
GetExtension = LCase(vExt)
End Function
Function IsValidShortcut(ByRef pPath, ByRef pDestPath, ByRef pWSH, ByRef pFS)
Dim oShortcut
Set oShortcut = pWSH.CreateShortcut(pPath)
pDestPath = oShortcut.TargetPath
IsValidShortcut = pFS.FileExists(pDestPath) Or pFS.FolderExists(pDestPath)
Set oShortcut = Nothing
End Function
Function DoSubFolders(ByRef pFolder, ByRef pOutput)
Dim oFile, oSubFolder
Dim vReturnValue
vReturnValue = EXIT_WITH_OK
For Each oFile In pFolder.Files
' ショートカットファイル検出
Dim vExt
vExt = GetExtension(oFile.Path)
If (vExt = "lnk") Then
Dim bCheck, vDestPath
bCheck = IsValidShortcut(oFile.Path, vDestPath, fWSH, fFS)
If (Len(pOutput) > 0) Then
pOutput = pOutput & vbCrLf
End If
If (bCheck) Then
pOutput = pOutput & "OK" & vbTab & """" & oFile.Path & """" & vbTab & """" & vDestPath & """"
Else
pOutput = pOutput & "NG" & vbTab & """" & oFile.Path & """" & vbTab & """" & vDestPath & """"
End If
End If
Next
For Each oSubFolder In pFolder.SubFolders
vReturnValue = DoSubFolders(oSubFolder, pOutput)
Next
DoSubFolders = vReturnValue
End Function
'///////////////////////////////////////////////////////////////////////////////
Dim fFS, fWSH, fTargetFolder
Set fWSH = WScript.CreateObject("WScript.Shell")
Set fFS = WScript.CreateObject("Scripting.FileSystemObject")
' コマンドライン引数の取得
Dim fTargetPath
If (WScript.Arguments.Count >= 1) Then
fTargetPath =WScript.Arguments.Item(0)
Else
fTargetPath = "."
End If
' 対象フォルダの取得
If (fFS.FolderExists(fTargetPath)) Then
Set fTargetFolder = fFS.GetFolder(fTargetPath)
Else
Call WScript.Echo("指定した " & fTargetPath & " は存在しません。")
Call WScript.Quit(EXIT_WITH_APPLICATION_ERROR)
End If
' 検査処理
Dim vResult, vOutput
vOutput = ""
vResult = DoSubFolders(fTargetFolder, vOutput)
Call WScript.Echo(vOutput)
' 終了処理
Set fFS = Nothing
Set fWSH = Nothing
Set fTargetFolder = Nothing
Call WScript.Quit(EXIT_WITH_OK)
最終更新:2009年12月05日 10:09