'This program needs a Common Dialog Box, named CDBox.
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim SHFileOp As SHFILEOPSTRUCT
'Set the dialog's title
CDBox.DialogTitle = "Select a file to delete ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File' dialog
CDBox.ShowOpen
With SHFileOp
'Delete the file
.wFunc = FO_DELETE
'Select the file
.pFrom = CDBox.filename
'Allow 'move to recycle bn'
.fFlags = FOF_ALLOWUNDO
End With
'perform file operation
SHFileOperation SHFileOp
MsgBox "The file '" + CDBox.filename + "' has been moved to your Recycling Bin !", vbInformation + vbOKOnly, App.Title
End Sub
' (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
' and select Microsoft Common Dialog control)
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_DELETE = &H3
Private Const FOF_ALLOWUNDO = &H40
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim SHFileOp As SHFILEOPSTRUCT
'Set the dialog's title
CDBox.DialogTitle = "Select a file to delete ..."
'Set the dialog's filter
CDBox.Filter = "All Files (*.*)|*.*"
'Show the 'Open File' dialog
CDBox.ShowOpen
With SHFileOp
'Delete the file
.wFunc = FO_DELETE
'Select the file
.pFrom = CDBox.filename
'Allow 'move to recycle bn'
.fFlags = FOF_ALLOWUNDO
End With
'perform file operation
SHFileOperation SHFileOp
MsgBox "The file '" + CDBox.filename + "' has been moved to your Recycling Bin !", vbInformation + vbOKOnly, App.Title
End Sub
Private Const FO_COPY = &H2
Private Const FO_DELETE = &H3
Private Const FO_RENAME = &H4
Private Const FOF_NOCONFIRMATION = &H10
Private Const FOF_SILENT = &H4
Private Const FOF_NOERRORUI = &H400
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
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 TypePublic Function ReName(ByVal lfrmhWnd As Long, ByVal sFrom As String, ByVal sTo As String, ByVal fShowProgress As Boolean) As Boolean
Dim udtPath As SHFILEOPSTRUCT
udtPath.hwnd = lfrmhWnd
udtPath.wFunc = FO_RENAME
udtPath.pFrom = sFrom
udtPath.pTo = sTo
udtPath.fFlags = FOF_NOCONFIRMATION Or FOF_NOERRORUI Or IIf(fShowProgress, 0, FOF_SILENT)
ReName = Not CBool(SHFileOperation(udtPath))
End FunctionPrivate Sub Form_Load()
'重命名目录
Call ReName(0, "F:\VBCODE", "F:\VBCODE1", False)
'重命名文件
Call ReName(0, "F:\SN.txt", "F:\SN.BAK", False)
End Sub
本示例使用 Name 语句来更改文件的名称。示例中假设所有使用到的目录或文件夹都已存在。Dim OldName, NewName
OldName = "OLDFILE": NewName = "NEWFILE" ' 定义文件名。
Name OldName As NewName ' 更改文件名。 OldName = "C:\MYDIR\OLDFILE": NewName = "C:\YOURDIR\NEWFILE"
Name OldName As NewName ' 更改文件名,并移动文件。