用VB完成文件改名
發(fā)表時(shí)間:2023-07-29 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]方法一name重新命名一個(gè)文件、目錄、或文件夾。語法:name oldpathname as newpathname Name語句重新命名文件并將其移動(dòng)到一個(gè)不同的目錄或文件夾中。如有必要,Name...
方法一
name
重新命名一個(gè)文件、目錄、或文件夾。
語法:
name oldpathname as newpathname
Name語句重新命名文件并將其移動(dòng)到一個(gè)不同的目錄或文件夾中。如有必要,Name可跨驅(qū)動(dòng)器移動(dòng)文件。但當(dāng)newpathname和oldpathname都在相同的驅(qū)動(dòng)器中時(shí),只能重新命名已經(jīng)存在的目錄或文件夾。Name不能創(chuàng)建新文件、目錄或文件夾。在一個(gè)已打開的文件上使用Name,將會(huì)產(chǎn)生錯(cuò)誤。必須在改變名稱之前,先關(guān)閉打開的文件。Name參數(shù)不能包括多字符(*)和單字符(?)的通配符。
示例本示例使用Name語句來更改文件的名稱。示例中假設(shè)所有使用到的目錄或文件夾都已存在。在Macintosh中,默認(rèn)驅(qū)動(dòng)器名稱是C, imOldName,NewNameOldName="OLDFILE":NewName="NEWFILE"注釋:定義文件名。NameOldNameAsNewName注釋:更改文件名。OldName="C:\MYDIR\OLDFILE":NewName="C:\YOURDIR\NEWFILE"NameOldNameAsNewName注釋:更改文件名,并移動(dòng)文件。
★★★★★★★★★★★★★★★★★★★★★★★★★★
方法二
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
可運(yùn)行任何DOS命令,2000下需用cmd.exe