File io 重命名以特定模式结尾的所有文件

File io 重命名以特定模式结尾的所有文件,file-io,vb6,File Io,Vb6,我在一个目录中有许多文件,它们的名称以变量模式结尾。我想把它们改短一点。文件名的格式如下: ************_A.tif ************_B.tif ************_C.tif 并将其命名为: A.tif B.tif C.tif 看看Dir和Name函数 例如: Private Sub Command1_Click() Dim strPath As String Dim strFile As String Dim strNew As String

我在一个目录中有许多文件,它们的名称以变量模式结尾。我想把它们改短一点。文件名的格式如下:

************_A.tif
************_B.tif
************_C.tif
并将其命名为:

A.tif
B.tif
C.tif

看看Dir和Name函数

例如:

Private Sub Command1_Click()
  Dim strPath As String
  Dim strFile As String
  Dim strNew As String
  Dim strType As String
  Dim lngLen As Long
  'set path
  strPath = "c:\temp\test"
  'set file type
  strType = ".tif"
  'set pattern length
  lngLen = Len(strType) + 1
  'find first file
  strFile = Dir$(strPath & "\*" & strType)
  'loop through all files of the selected type
  Do Until Len(strFile) = 0
    'show current filename
    Print "File : " & strFile
    'create new filename
    strNew = Right$(strFile, lngLen)
    'show new filename
    Print "New name : " & strNew
    'rename file to new filename
    Name strPath & "\" & strFile As strPath & "\" & strNew
    'find next file
    strFile = Dir$()
  Loop
End Sub

您可能必须更改创建新文件名的行,因为可能有文件具有相同的新名称

如果文件夹中有两个文件以相同的模式结尾,该怎么办?像
abc123_A.tif
123abc_A.tif