If file exists then delete the file
Asked Answered
B

3

8

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 example: I have a batch of files that are named like this 11111111.dddddddd.pdf where the files get renamed to 11111111.pdf. The problem is that when I rename to the 11111111.pdf format I end of with files that are duplicated and then makes the script fail because you obviously cant have 2 files with the same name. I need it to rename the first one but then delete the others that are renamed the same.

Here is what I have so far for my IF statement but it doesnt work and I get and error that says "Type mismatch: 'FileExists". I am not sure how to get this part of the code to execute the way I would like. Any help or suggestions would be greatly appreciated.

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, ".")
dim acct_, date_
acct_ = parts(0)
date_ = parts(1)


' file format of a.c.pdf
if UBound(parts) = 2 then
    ' rebuild the name with the 0th and 2nd elements
    dim newname: newname = acct_ & "." & parts(2)
    ' use the move() method to effect the rename
    file.move fso.buildpath(OUT_PATH, newname)  

    if  newname = FileExists(file.name) Then            
    newname.DeleteFile()
    end if   
end if

next 'file
Babysitter answered 26/1, 2012 at 0:36 Comment(0)
A
15

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
Armorial answered 26/1, 2012 at 7:30 Comment(1)
Salman A- I knew that it had to be something simple. I got all worked up over it and wasnt thinking straight. Works like a charm. Much appreciated.Babysitter
G
4

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.

Georganngeorge answered 26/1, 2012 at 7:10 Comment(1)
Thanks for your response. I tested this as well and it works.Babysitter
T
1
IF both POS_History_bim_data_*.zip and POS_History_bim_data_*.zip.trg exists in  Y:\ExternalData\RSIDest\ Folder then Delete File Y:\ExternalData\RSIDest\Target_slpos_unzip_done.dat
Tinct answered 5/11, 2012 at 16:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.