vbscript - If FileExists delete another file -
i trying add sub-routine vbscript. in short, trying see if 1 type of file exists, delete file.
there files like:
socal_cu59_res.dxf socal_cu59_main.dxf socal_cu59_mot.dxf socal_cu59_motl.dxf
but on occassion there may file "x" @ end of filename:
socal_cu59_resx.dxf socal_cu59_mainx.dxf socal_cu59_motx.dxf socal_cu59_motlx.dxf
they in same folder. "x" file has priority. if exist want delete matching file file without "x".
here have far errors. check filesize routine added works great it's after having no luck:
dim ofso, sdirectorypath, ofolder, ofile set ofso = createobject("scripting.filesystemobject") sdirectorypath = "s:\socal\section_11\road dxfs\" recursefolders sdirectorypath sub recursefolders(sfolder) 'here set ofolder object, note variable scope within 'this sub, can set many times , it's value 'that of sub that's running. set ofolder = ofso.getfolder(sfolder) 'here looping through every file in directory path. each ofile in ofolder.files 'this checks file size less 100kb if ofile.size <= 1085 , right(lcase(ofile.name),3) = "dxf" ofile.delete true end if next each ofile in ofolder.files 'this checks if there file 'x' @ end of filename if fileexists (right(ofile.name),1) = "x" ofile.delete true end if next 'here recursive bit. need loop through each folder in 'the directory , call same sub ensure check every folder 'in path. each ofolder in ofolder.subfolders recursefolders ofolder.path next end sub the script creates both files, not delete file not have "x". error says line 204, char 5:
wrong number of arguments or invalid property assignment: 'right'
the line error refers is: if fileexists (right(ofile.name),1) = "x" then.
you have few inherent problems need correct in order properly. first, need make parenthesis correction mentioned ansgar wiechers. second, should remove duplicate loop. there's no need loop on of files multiple times. finally, should store files deleted until after loop has finished. deleting file while in file set being looped on produce unexpected results or unexplained errors.
with said, here's how approach this. you'll note of corrections i've mentioned.
dim ofso, sdirectorypath, ofolder, ofile set ofso = createobject("scripting.filesystemobject") sdirectorypath = "s:\socal\section_11\road dxfs\" dim arrfilestodelete() 'an empty dynamic array hold files deleted later dim = 0 'an iterator used track array pointer recursefolders sdirectorypath deleteextrafiles arrfilestodelete sub recursefolders(sfolder) 'here set ofolder object, note variable scope within 'this sub, can set many times , it's value 'that of sub that's running. set ofolder = ofso.getfolder(sfolder) 'here looping through every file in directory path. each ofile in ofolder.files 'is file "dxf" file if lcase(right(ofile.name)) = "dxf" 'this checks file size less 100kb if ofile.size <= 1085 , right(lcase(ofile.name),3) = "dxf" end if 'this checks if there 'x' @ end of filename if lcase(right(ofile.name) 5) = "x.dxf" 'if so, store counterpart deletion later sbadfile = replace(ofile.name, "x.dxf", ".dxf") redim preserve arrfilestodelete(i) arrfilestodelete(i) = ofile.path & "\" & sbadfile = + 1 end if end if next 'here recursive bit. need loop through each folder in 'the directory , call same sub ensure check every folder 'in path. each ofolder in ofolder.subfolders recursefolders ofolder.path next end sub sub deleteextrafiles(arrfiles) each sfile in arrfiles if ofso.fileexists(sfile) ofso.deletefile sfile end if next end sub
Comments
Post a Comment