方法一
name 重新命名一個文件、目錄、或文件夾。 語法: name oldpathname as newpathname
Name語句重新命名文件并將其移動到一個不同的目錄或文件夾中。如有必要,Name可跨驅動器移動文件。但當newpathname和oldpathname都在相同的驅動器中時,只能重新命名已經存在的目錄或文件夾。Name不能創建新文件、目錄或文件夾。在一個已打開的文件上使用Name,將會產生錯誤。必須在改變名稱之前,先關閉打開的文件。Name參數不能包括多字符(*)和單字符(?)的通配符。
示例本示例使用Name語句來更改文件的名稱。示例中假設所有使用到的目錄或文件夾都已存在。在Macintosh中,默認驅動器名稱是C, imOldName,NewNameOldName="OLDFILE":NewName="NEWFILE"注釋:定義文件名。NameOldNameAsNewName注釋:更改文件名。OldName="C:\MYDIR\OLDFILE":NewName="C:\YOURDIR\NEWFILE"NameOldNameAsNewName注釋:更改文件名,并移動文件。 ★★★★★★★★★★★★★★★★★★★★★★★★★★
方法二 Option Explicit
注釋: // Shell File Operations
Const FO_MOVE = &H1 Const FO_COPY = &H2 Const FO_DELETE = &H3 Const FO_RENAME = &H4 Const FOF_MULTIDESTFILES = &H1 Const FOF_CONFIRMMOUSE = &H2 Const FOF_SILENT = &H4 注釋: don注釋:t create progress/report Const FOF_RENAMEONCOLLISION = &H8 Const FOF_NOCONFIRMATION = &H10 注釋: Don注釋:t prompt the user. Const FOF_WANTMAPPINGHANDLE = &H20 注釋: Fill in SHFILEOPSTRUCT.hNameMappings 注釋: Must be freed using SHFreeNameMappings Const FOF_ALLOWUNDO = &H40 Const FOF_FILESONLY = &H80 注釋: on *.*, do only files Const FOF_SIMPLEPROGRESS = &H100 注釋: means don注釋:t show names of files Const FOF_NOCONFIRMMKDIR = &H200 注釋: don注釋:t confirm making any needed dirs
Const PO_DELETE = &H13 注釋: printer is being deleted Const PO_RENAME = &H14 注釋: printer is being renamed Const PO_PORTCHANGE = &H20 注釋: port this printer connected to is being changed 注釋: if this id is set, the strings received by 注釋: the copyhook are a doubly-null terminated 注釋: list of strings. The first is the printer 注釋: name and the second is the printer port. Const PO_REN_PORT = &H34 注釋: PO_RENAME and PO_PORTCHANGE at same time.
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
注釋:Private Declare Function SHFileOperation Lib "shell32.dll" Alias " SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Declare Function SHFileOperation Lib "shell32.dll" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Command1_Click() Dim tud As SHFILEOPSTRUCT Dim rc As Long With tud .hwnd = Me.hwnd .pFrom = "c:\aaa.txt" .pTo = "c:\bbb.txt" .fFlags = FOF_ALLOWUNDO .wFunc = FO_RENAME End With rc = SHFileOperation(tud) End Sub ★★★★★★★★★★★★★★★★★★★★★★★★★★
方法三 Private Sub Command1_Click() Dim icmd As Object Set icmd = CreateObject("wscript.shell") icmd.Run "command.com /c rename c:\test.txt aa.txt", 0, True End Sub
可運行任何DOS命令,2000下需用cmd.exe
|