0

While making a batch file, I thought of adding a version number to the name so new versions don't get replaced by old. What I want the new version to do is delete all the old ones, so as an example lets take a folder named apps and in it are Test (v1).bat, Test (v2).bat & Test (v3).bat. What I want Test (v3).bat to do before anything else is delete Test (v1).bat & Test (v2).bat, I know what you might be thinking at this point:

Why don't you just add the lines ( del "Test (v1).bat" ) & ( del "Test (v2).bat" )?

That is not what I want, because lets say after a while I make Test (v4).bat and want it to delete all previous versions if they exist. Obviously it wont be very optimized if I will constantly have to add & not forget to add del "Test (vā„–).bat". If I do something like this del "*.bat" then all files with a .bat extension will be deleted, even the currently executed one, so that doesn't help. Also I want it so if a old version is executed, then it wont delete a newer version (if Test (v2).bat is executed don't delete Test (v3).bat, Test (v4).bat, etc., but delete Test (v1).bat)

Gecko64
  • 47
  • 1
  • 5
  • If you're deleting the old ones anyway why not just overwrite in the first place or simply name the newest version `Test.cmd` and keep `Test.bat` as the previous version. – Compo Aug 17 '17 at 21:53
  • I can see a few reasons; it could be that the old versions get mirrored off for backup purposes, but only the latest version is to be kept on the system once used. – Steven K. Mariner Aug 17 '17 at 23:56

1 Answers1

0

It is dependent on having a rigorous naming standard that won't be co-opted by someone (such assumptions are at the heart of many security and prank attacks, so be careful).

This one is written assuming the current batch file already conforms to the naming standard implied by your question. I did not code a safety into this to avoid a suicidal rampage in the event that the file is not compliant; you can do that for yourself. :-)

Also, the goto ENDIT in this case is superfluous but in case you had other code to add, or subroutines, or what have you, this is a safety best-practice I do in all my batch files. You can remove both the goto ENDIT and the :ENDIT labels for a miniscule performance improvement.

Change the echo del to del once you have confirmed this thing isn't going to kill you in unexpected ways.

@echo off
set PUR_CURFNM=
for /F "usebackq tokens=*" %%F in ('%0') do set PUR_CURFNM=%%~F
for %%F in ("%PUR_CURFNM%") do set PUR_FULFNM=%%~dpnxF
for %%F in ("%PUR_CURFNM%") do set PUR_BASBNM=%%~dpnF
set PUR_BASFNM=
for /F "tokens=1 delims=(" %%A in ("%PUR_BASBNM%") do set PUR_BASFNM=%%A
for %%F in ("%PUR_BASFNM%(v*).bat") do if /I "%%F" NEQ "%PUR_FULFNM%" echo del "%%F"

goto ENDIT

:ENDIT
set PUR_FULFNM=
set PUR_CURFNM=
set PUR_BASBNM=
set PUR_BASFNM=

I'm with @Compo, though; this is a kind of odd request. Have fun with it. :-)

  • I just realized I skipped a requirement: You don't want to delete files with a higher version number. That will take some additional work using `FOR /F`, `SET`, and `SET /A`. – Steven K. Mariner Aug 18 '17 at 14:15