How do I rename a file using VBScript?

后端 未结 7 1931
北恋
北恋 2020-12-11 01:44

I am trying to rename a file and was using the below code but it does not seem to work. Can someone please tell me why? What is the correct way to rename a file from VBScrip

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-11 02:29

    Below code absolutely worked for me to update File extension.

    Ex: abc.pdf to abc.txt

    Filepath = "Pls mention your Filepath"
    
    Set objFso = CreateObject("Scripting.FileSystemObject")
    
    '' Below line of code is to get the object for Folder where list of files are located 
    Set objFolder = objFso.GetFolder(Filepath)
    
    '' Below line of code used to get the collection object to hold list of files located in the Filepath.
    Set FileCollection = objFolder.Files
    
    For Each file In FileCollection
    
        WScript.Echo "File name ->" + file.Name
        ''Instr used to Return the position of the first occurrence of "." within the File name
        s = InStr(1, file.Name, ".",1)
        WScript.Echo s
        WScript.Echo "Extn --> " + Mid(file.Name, s, Len(file.Name))
    
        'Left(file.Name,s-1) = Used to fetch the file name without extension
        ' Move method is used to move the file in the Desitnation folder you mentioned
        file.Move(Filepath & Left(file.Name,s-1)&".txt") 
    
    Next
    

提交回复
热议问题