If the file exists, delete the file.

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

For example: I have a package of files called 11111111.dddddddd.pdf, where the files are renamed to 11111111.pdf. The problem is that when I rename the format 11111111.pdf, I end up with copies of files that are duplicated, and then the script crashes because you obviously do not have two files with the same name. I need him to rename the first one and then delete the rest that were renamed.

Here is what I have so far used for my IF statement, but it doesn’t work, and I get the error β€œType of mismatch:β€œ FileExists. ”I’m not sure how to make this part of the code execute as 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 acpdf 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 
+4
source share
3 answers

You're close, you just need to delete the file before trying to overwrite 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 acpdf 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 
+12
source

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

You also have a delete problem, DeleteFile() also a FileSystemObject method.

Also, it seems you are moving the file and then trying to solve the rewriting problem, which is out of order. You must first define a name collision so that you can first transfer the file or delete the collision. I assume that for some reason you want to keep deleting new files until you get to the last one, which was apparently implied in your question.

So you can 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 note that string comparison with the = sign is case sensitive. Use strCmp with the strCmp option to compare vbText strings.

+4
source
 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 
+1
source

Source: https://habr.com/ru/post/1393070/


All Articles