ファイルのコピー・削除2
ファイルのコピー・削除1との違いはファイルをまとめて扱う効率性と、ダイアログの表示などの付加操作にある。こちらで解説するほうが高度な例なので必要なければ使う必要はない。
概要ファイルをまとめてコピーする方法 → サンプル1 ワイルドカードで指定してコピーする方法 → サンプル2 ファイルをまとめて削除する方法 → サンプル3 ファイルをまとめてゴミ箱に移す方法 → サンプル4 進捗状況をダイアログの表示する方法 → サンプル1〜4 |
C:\CopySource\フォルダにあるすべてのファイルをC:\CopyTargetフォルダにコピーする。
時間がかかる場合にはコピー中のダイアログが自動的に表示される。
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer Private Structure SHFILEOPSTRUCT
Public hwnd As Integer
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Integer
Public hNameMappings As Integer
Public lpszProgressTitle As String
End StructurePrivate
Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_MOVE = &H1
Private Const FOF_ALLOWUNDO = &H40Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ret As Integer
Dim sf As SHFILEOPSTRUCTsf.hwnd = Me.Handle.ToInt32
sf.wFunc = FO_COPY
sf.pFrom = "C:\CopySource\*.*" 'コピー元
sf.pTo = "C:\CopyTarget" 'コピー先Ret = SHFileOperation(sf)
If Ret <> 0 Then MsgBox("失敗しました。")
End Sub
▲サンプル1
上記「1.コピーの単純な例」を一箇所変更するだけ。変更箇所には色がつけてある。
ここでは例として bmp ファイルのみをコピーする。
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer Private Structure SHFILEOPSTRUCT
Public hwnd As Integer
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Integer
Public hNameMappings As Integer
Public lpszProgressTitle As String
End StructurePrivate
Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_MOVE = &H1
Private Const FOF_ALLOWUNDO = &H40Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ret As Integer
Dim sf As SHFILEOPSTRUCTsf.hwnd = Me.Handle.ToInt32
sf.wFunc = FO_COPY
sf.pFrom = "C:\CopySource\*.bmp" 'コピー元
sf.pTo = "C:\CopyTarget" 'コピー先Ret = SHFileOperation(sf)
If Ret <> 0 Then MsgBox("失敗しました。")
End Sub
▲サンプル2
上記「1.コピーの単純な例」を2箇所変更するだけ。1箇所には色がつけてある。もう1箇所はあってもなくても良いのだが、sf.pTo = の行が不要になるので下記の例では削除してある。
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer Private Structure SHFILEOPSTRUCT
Public hwnd As Integer
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Integer
Public hNameMappings As Integer
Public lpszProgressTitle As String
End StructurePrivate
Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_MOVE = &H1
Private Const FOF_ALLOWUNDO = &H40Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ret As Integer
Dim sf As SHFILEOPSTRUCTsf.hwnd = Me.Handle.ToInt32
sf.wFunc = FO_DELETE
sf.pFrom = "C:\DeleteSource\*.*" '削除元Ret = SHFileOperation(sf)
If Ret <> 0 Then MsgBox("失敗しました。")
End Sub
▲サンプル3
上記「3.削除の単純な例」に1箇所追加するだけ。追加箇所には色がつけてある。
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" (ByRef lpFileOp As SHFILEOPSTRUCT) As Integer Private Structure SHFILEOPSTRUCT
Public hwnd As Integer
Public wFunc As Integer
Public pFrom As String
Public pTo As String
Public fFlags As Short
Public fAnyOperationsAborted As Integer
Public hNameMappings As Integer
Public lpszProgressTitle As String
End StructurePrivate
Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_MOVE = &H1
Private Const FOF_ALLOWUNDO = &H40Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ret As Integer
Dim sf As SHFILEOPSTRUCTsf.hwnd = Me.Handle.ToInt32
sf.wFunc = FO_DELETE
sf.fFlags = FOF_ALLOWUNDO
sf.pFrom = "C:\DeleteSource\*.*" '削除元Ret = SHFileOperation(sf)
If Ret <> 0 Then MsgBox("失敗しました。")
End Sub
▲サンプル4