首页 文章

VBScript重命名文件夹中的特定前缀文件

提问于
浏览
1

我已经成功创建了一个VBScript,它在文件夹中唯一的文件时根据需要重命名文件 . 我无法弄清楚如何让脚本搜索过去最近的文件 .

Option Explicit

 Dim fso, folder, file,  tmFile
 Dim folderName

 folderName     = "\\pcc\Int\PC\Inbox\"

 Set fso = CreateObject("Scripting.FileSystemObject")  
 Set folder = fso.GetFolder(folderName)  
 Set tmFile = Nothing

 For each file In folder.Files    

 If (tmFile is Nothing) Then 
     Set tmFile = file
     Exit For
 End IF

 Next

 If InStr(tmfile.name, "TM") Then
     TmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
 End if

上面的脚本正确地重命名了该文件 .

以下是我尝试浏览文件夹中所有文件以搜索具有前缀TM的文件的一些修改 . 这将始终是具有TM前缀的唯一文件 .

For Each InStr(tmFile.name, "TM")  Then 
    tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
 Exit for

If tmFile.fileexists(tmFile.name, "TM")  Then 
     tmFile.Name = Replace(tmFile.Name, ".txt", "A.txt")
 End if

1 回答

  • 1

    你已经接近了 instr() ,只需要在现有的 For 循环中进行测试:

    Option Explicit
    
     Dim fso, folder, file,  tmFile
     Dim folderName
    
     folderName     = "\\pcc\Int\PC\Inbox\"
    
     Set fso = CreateObject("Scripting.FileSystemObject")  
     Set folder = fso.GetFolder(folderName)  
    
     For each file In folder.Files  
         If instr(file, "TM") > 0 THEN
             file.name = replace(file.name, ".txt". "A.txt")
         End IF
     Next
    

    我删除了 tmfile 变量,因为这里根本不需要它 .

相关问题