If file exists then delete the file

前端 未结 3 2154
傲寒
傲寒 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 06:40

    fileExists() is a method of FileSystemObject, not a global scope function.

    You also have an issue with the delete, DeleteFile() is also a method of FileSystemObject.

    Furthermore, it seems you are moving the file and then attempting to deal with the overwrite issue, which is out of order. First you must detect the name collision, so you can choose the rename the file or delete the collision first. I am assuming for some reason you want to keep deleting the new files until you get to the last one, which seemed implied in your question.

    So you could use the block:

    if NOT fso.FileExists(newname) Then  
    
        file.move fso.buildpath(OUT_PATH, newname)           
    
    else
    
        fso.DeleteFile newname
        file.move fso.buildpath(OUT_PATH, newname)  
    
    end if 
    

    Also be careful that your string comparison with the = sign is case sensitive. Use strCmp with vbText compare option for case insensitive string comparison.

提交回复
热议问题