If file exists then delete the file

前端 未结 3 2155
傲寒
傲寒 2021-01-05 06:33

I have a vbscript that is used to rename files. What I need to implement into the script is something that deletes the \"new file\" if it already exists.

For exampl

3条回答
  •  一个人的身影
    2021-01-05 07:02

    You're close, you just need to delete the file before trying to over-write it.

    dim infolder: set infolder = fso.GetFolder(IN_PATH)
    dim file: for each file in infolder.Files
    
        dim name: name = file.name
        dim parts: parts = split(name, ".")
    
        if UBound(parts) = 2 then
    
           ' file name like a.c.pdf    
    
            dim newname: newname = parts(0) & "." & parts(2)
            dim newpath: newpath = fso.BuildPath(OUT_PATH, newname)
    
            ' warning:
            ' if we have source files C:\IN_PATH\ABC.01.PDF, C:\IN_PATH\ABC.02.PDF, ...
            ' only one of them will be saved as D:\OUT_PATH\ABC.PDF
    
            if fso.FileExists(newpath) then
                fso.DeleteFile newpath
            end if
    
            file.Move newpath
    
        end if
    
    next
    

提交回复
热议问题