Batch File - Delete a File only if another file exists -


i wish create batch file can following:

if "file 1abc (x).bin" exists delete "file 1abc (y).bin", if not, keep "file 1abc (y).bin" , move on.

basically there multiple versions of file 1 kind (marked single variable in filename stays same) superior other. if superior version not exist, wish inferior version remain.

is possible?

you use if statement (http://www.computerhope.com/if.htm):

if exist file1.ext del file2.ext 

update - address specific problem mentioned in question:

set filenames="file 1abc (*).bin" set lastversion="" :: find last version %%f in (%filenames%) (     if "%%f" gtr %lastversion% set lastversion="%%f" ) :: delete old versions %%f in (%filenames%) (     if "%%f" lss %lastversion% del "%%f" ) 

there 1 catch: if version (x or y in example) number, should specified fixed number of digits (e.g. (009), (010), ...), because (9) considered greater (10).


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -