作者: 王国荣 Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Long hNameMappings As Long lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS End Type 'wFunc 的设定值 'FO_COPY Copies the files specified by pFrom to the location specified by pTo. 'FO_DELETE Deletes the files specified by pFrom (pTo is ignored). 'FO_MOVE Moves the files specified by pFrom to the location specified by pTo. 'FO_RENAME Renames the files specified by pFrom.'fFlag的设定 'FOF_ALLOWUNDO Preserves undo information, if possible. 'FOF_FILESONLY Performs the operation only on files if a wildcard filename ' (*.*) is specified. 'FOF_MULTIDESTFILES Indicates that the pTo member specifies multiple destination ' files (one for each source file) rather than one directory where ' all source files are to be deposited. 'FOF_NOCONFIRMATION Responds with "yes to all" for any dialog box that is displayed. 'FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if ' the operation requires one to be created. 'FOF_RENAMEONCOLLISION Gives the file being operated on a new name (such as ' "Copy #1 of...") in a move, copy, or rename operation ' if a file of the target name already exists. 'FOF_SILENT Does not display a progress dialog box. 'FOF_SIMPLEPROGRESS Displays a progress dialog box, but does not show the ' filenames. 'FOF_WANTMAPPINGHANDLE Fills in the hNameMappings member. The handle must be ' freed by using the SHFreeNameMappings function.Const FO_MOVE = &H1 Const FO_COPY = &H2 Const FO_DELETE = &H3 Const FOF_NOCONFIRMATION = &H10 Const FOF_NOCONFIRMMKDIR = &H200 Const FOF_ALLOWUNDO = &H40 Const FOF_SILENT = &H4 Private Declare Function SHFileOperation Lib "shell32.dll" Alias _ "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long'删除 test目录及其底下的子目录到资源回收桶 Private Sub Command1_Click() Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_DELETE SHFileOp.pFrom = "c:\test" + Chr(0) '不出现档案删除的动态AVI,且不Confirm SHFileOp.fFlags = FOF_SILENT + FOF_ALLOWUNDO + FOF_NOCONFIRMATION '若没有 FOF_ALLOWUNDO 则不会到资源回收桶 Call SHFileOperation(SHFileOp) End Sub'同时删除多档到资源回收桶 Private Sub Command2_Click() Dim SHFileOp As SHFILEOPSTRUCT Dim Files As String 'Files = "c:\test.txt" + Chr(0) Files = "c:\test1.txt" + Chr(0) + "c:\test2.txt" + Chr(0) + _ "c:\test3.txt" + Chr(0) SHFileOp.wFunc = FO_DELETE SHFileOp.pFrom = Files '删至资源回收桶,且不Confirm SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION Call SHFileOperation(SHFileOp) End Sub'将 c:\temp 整个目录复制到 c:\temp2 Private Sub Command3_Click() Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_COPY SHFileOp.pFrom = "c:\temp\*.*" SHFileOp.pTo = "c:\temp2\*.*" SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR Call SHFileOperation(SHFileOp) End Sub'将 c:\test4.txt 快速移到 c:\temp 目录 Private Sub Command4_Click() Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_MOVE SHFileOp.pFrom = "c:\test4.txt" + Chr(0) SHFileOp.pTo = "c:\temp" SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION Call SHFileOperation(SHFileOp) End Sub
定义如下 Dim object Set object= CreateObject("Scripting.FileSystemObject")
看这个简单的:1。读取文件 Open "来源文件绝对路径" For Binary As #1 ReDim Bit(LOF(1)) As Byte Get 1, 1, Bit Close 1 2。写入(复制)文件 '写入 Open "目的文件绝对路径" For Binary As #1 Put 1, 1, Bit Close 1 Unload Form10这种方法简单并且有个好处,不存在文件独占问题,就是说你可以这样复制一个正在OPEN状态的文件!
'Use FSO to Copy File and Move File 'if you add the reference of "Microsoft Scripting Runtime" 'You can Define Like This ' Dim strFullPath1 As String ' Dim strFullPath2 As String ' Dim strFullPath3 As String ' Dim fs As New FileSystemObject ' Dim TargetFolder As Folder ' Dim TargetFile As String ' Dim FileSets As Files ' Dim strMsg As StringFunction CopyFile() Dim strFullPath1 as String Dim strFullPath2 as String Dim strFullPath3 as String Dim fs,TargetFolder, TargetFile, FileSets, strMsg
Set fs = CreateObject("Scripting.FileSystemObject") strFullPath1 = "C:\Temp" strFullPath2 = "C:\TextBackup" strFullPath3 = "C:\TextTrash" ' 先检查磁盘目录 C:\Temp 是否存在, ' 如果不存在则建立此磁盘目录。 If Not fs.FolderExists(strFullPath1) Then fs.CreateFolder(strFullPath1) End If ' 先检查磁盘目录 C:\TextBackup 是否存在, ' 如果不存在则建立此磁盘目录。 If Not fs.FolderExists(strFullPath2) Then fs.CreateFolder(strFullPath2) End If ' 不论磁盘目录 C:\TextTrash 是否存在, ' 都重新建立此磁盘目录。 If fs.FolderExists(strFullPath3) Then fs.DeleteFolder(strFullPath3) End If fs.CreateFolder(strFullPath3) Set TargetFolder = fs.GetFolder(strFullPath1) Set FileSets = TargetFolder.Files If FileSets.Count > 0 Then ' 将 C:\Temp 中的文本文件复制到 C:\TextBackup 中 fs.CopyFile strFullPath1 & "\*.TXT",strFullPath2 ' 将 C:\TextBackup 中的文本文件迁移至 C:\TextTrash 中 fs.MoveFile strFullPath2 & "\*.TXT",strFullPath3 Else MsgBox "磁盘目录 " & strFullPath1 & " 中没有任何文件",_ vbOKOnly+vbCritical, _ "计算机" End If Set fs = Nothing Set TargetFolder = Nothing Set FileSets = Nothing
End Function
你应该这样用: dim fs as object '定义一个对象Set fs = CreateObject("Scripting.FileSystemObject") '创建对象fs.copyfile source, destination '拷贝一个文件到一个目录 'source是源文件(带完整路径),destination是目标目录的完整路径
FileCopy 源文件路径,目的文件路径至于你说的FileSystemObject对象定义,可以这样:
“工程”-“引用”-“Microsoft Scripting Runtime”
下面建议另一个方法
是否对你有所帮助
如何用程序来Delete Copy Move Rename File/Directory
作者: 王国荣 Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
End Type
'wFunc 的设定值
'FO_COPY Copies the files specified by pFrom to the location specified by pTo.
'FO_DELETE Deletes the files specified by pFrom (pTo is ignored).
'FO_MOVE Moves the files specified by pFrom to the location specified by pTo.
'FO_RENAME Renames the files specified by pFrom.'fFlag的设定
'FOF_ALLOWUNDO Preserves undo information, if possible.
'FOF_FILESONLY Performs the operation only on files if a wildcard filename
' (*.*) is specified.
'FOF_MULTIDESTFILES Indicates that the pTo member specifies multiple destination
' files (one for each source file) rather than one directory where
' all source files are to be deposited.
'FOF_NOCONFIRMATION Responds with "yes to all" for any dialog box that is displayed.
'FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if
' the operation requires one to be created.
'FOF_RENAMEONCOLLISION Gives the file being operated on a new name (such as
' "Copy #1 of...") in a move, copy, or rename operation
' if a file of the target name already exists.
'FOF_SILENT Does not display a progress dialog box.
'FOF_SIMPLEPROGRESS Displays a progress dialog box, but does not show the
' filenames.
'FOF_WANTMAPPINGHANDLE Fills in the hNameMappings member. The handle must be
' freed by using the SHFreeNameMappings function.Const FO_MOVE = &H1
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FOF_NOCONFIRMATION = &H10
Const FOF_NOCONFIRMMKDIR = &H200
Const FOF_ALLOWUNDO = &H40
Const FOF_SILENT = &H4
Private Declare Function SHFileOperation Lib "shell32.dll" Alias _
"SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long'删除 test目录及其底下的子目录到资源回收桶
Private Sub Command1_Click()
Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = "c:\test" + Chr(0)
'不出现档案删除的动态AVI,且不Confirm
SHFileOp.fFlags = FOF_SILENT + FOF_ALLOWUNDO + FOF_NOCONFIRMATION
'若没有 FOF_ALLOWUNDO 则不会到资源回收桶
Call SHFileOperation(SHFileOp)
End Sub'同时删除多档到资源回收桶
Private Sub Command2_Click()
Dim SHFileOp As SHFILEOPSTRUCT
Dim Files As String
'Files = "c:\test.txt" + Chr(0)
Files = "c:\test1.txt" + Chr(0) + "c:\test2.txt" + Chr(0) + _
"c:\test3.txt" + Chr(0)
SHFileOp.wFunc = FO_DELETE
SHFileOp.pFrom = Files
'删至资源回收桶,且不Confirm
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
Call SHFileOperation(SHFileOp)
End Sub'将 c:\temp 整个目录复制到 c:\temp2
Private Sub Command3_Click()
Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_COPY
SHFileOp.pFrom = "c:\temp\*.*"
SHFileOp.pTo = "c:\temp2\*.*"
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMMKDIR
Call SHFileOperation(SHFileOp)
End Sub'将 c:\test4.txt 快速移到 c:\temp 目录
Private Sub Command4_Click()
Dim SHFileOp As SHFILEOPSTRUCT SHFileOp.wFunc = FO_MOVE
SHFileOp.pFrom = "c:\test4.txt" + Chr(0)
SHFileOp.pTo = "c:\temp"
SHFileOp.fFlags = FOF_ALLOWUNDO + FOF_NOCONFIRMATION
Call SHFileOperation(SHFileOp)
End Sub
Dim object
Set object= CreateObject("Scripting.FileSystemObject")
Open "来源文件绝对路径" For Binary As #1
ReDim Bit(LOF(1)) As Byte
Get 1, 1, Bit
Close 1
2。写入(复制)文件
'写入
Open "目的文件绝对路径" For Binary As #1
Put 1, 1, Bit
Close 1
Unload Form10这种方法简单并且有个好处,不存在文件独占问题,就是说你可以这样复制一个正在OPEN状态的文件!
'if you add the reference of "Microsoft Scripting Runtime"
'You can Define Like This
' Dim strFullPath1 As String
' Dim strFullPath2 As String
' Dim strFullPath3 As String
' Dim fs As New FileSystemObject
' Dim TargetFolder As Folder
' Dim TargetFile As String
' Dim FileSets As Files
' Dim strMsg As StringFunction CopyFile() Dim strFullPath1 as String
Dim strFullPath2 as String
Dim strFullPath3 as String
Dim fs,TargetFolder, TargetFile, FileSets, strMsg
Set fs = CreateObject("Scripting.FileSystemObject") strFullPath1 = "C:\Temp"
strFullPath2 = "C:\TextBackup"
strFullPath3 = "C:\TextTrash" ' 先检查磁盘目录 C:\Temp 是否存在,
' 如果不存在则建立此磁盘目录。
If Not fs.FolderExists(strFullPath1) Then
fs.CreateFolder(strFullPath1)
End If ' 先检查磁盘目录 C:\TextBackup 是否存在,
' 如果不存在则建立此磁盘目录。
If Not fs.FolderExists(strFullPath2) Then
fs.CreateFolder(strFullPath2)
End If ' 不论磁盘目录 C:\TextTrash 是否存在,
' 都重新建立此磁盘目录。
If fs.FolderExists(strFullPath3) Then
fs.DeleteFolder(strFullPath3)
End If
fs.CreateFolder(strFullPath3) Set TargetFolder = fs.GetFolder(strFullPath1)
Set FileSets = TargetFolder.Files If FileSets.Count > 0 Then
' 将 C:\Temp 中的文本文件复制到 C:\TextBackup 中
fs.CopyFile strFullPath1 & "\*.TXT",strFullPath2 ' 将 C:\TextBackup 中的文本文件迁移至 C:\TextTrash 中
fs.MoveFile strFullPath2 & "\*.TXT",strFullPath3
Else
MsgBox "磁盘目录 " & strFullPath1 & " 中没有任何文件",_
vbOKOnly+vbCritical, _
"计算机"
End If
Set fs = Nothing
Set TargetFolder = Nothing
Set FileSets = Nothing
End Function
dim fs as object '定义一个对象Set fs = CreateObject("Scripting.FileSystemObject") '创建对象fs.copyfile source, destination '拷贝一个文件到一个目录
'source是源文件(带完整路径),destination是目标目录的完整路径